Min Max Game - Problem
You are given a 0-indexed integer array nums whose length is a power of 2.
Apply the following algorithm on nums:
- Let
nbe the length ofnums. Ifn == 1, end the process. Otherwise, create a new 0-indexed integer arraynewNumsof lengthn / 2. - For every even index
iwhere0 <= i < n / 2, assign the value ofnewNums[i]asmin(nums[2 * i], nums[2 * i + 1]). - For every odd index
iwhere0 <= i < n / 2, assign the value ofnewNums[i]asmax(nums[2 * i], nums[2 * i + 1]). - Replace the array
numswithnewNums. - Repeat the entire process starting from step 1.
Return the last number that remains in nums after applying the algorithm.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,3,5,2,4,8,2,2]
›
Output:
1
💡 Note:
Round 1: [min(1,3), max(5,2), min(4,8), max(2,2)] = [1,5,4,2]. Round 2: [min(1,5), max(4,2)] = [1,4]. Round 3: min(1,4) = 1
Example 2 — Smaller Array
$
Input:
nums = [3]
›
Output:
3
💡 Note:
Array has only one element, so return it directly
Example 3 — Two Elements
$
Input:
nums = [9,3]
›
Output:
3
💡 Note:
Even index 0: min(9,3) = 3
Constraints
- 1 ≤ nums.length ≤ 1024
- nums.length is a power of 2
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Start with power-of-2 length array
2
Apply Rules
Even indices take min, odd indices take max of pairs
3
Final Result
Continue until one element remains
Key Takeaway
🎯 Key Insight: The algorithm simulates a tournament where even positions favor smaller values (min) and odd positions favor larger values (max)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code