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:

  1. Let n be the length of nums. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n / 2.
  2. For every even index i where 0 <= i < n / 2, assign the value of newNums[i] as min(nums[2 * i], nums[2 * i + 1]).
  3. For every odd index i where 0 <= i < n / 2, assign the value of newNums[i] as max(nums[2 * i], nums[2 * i + 1]).
  4. Replace the array nums with newNums.
  5. 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
Min-Max Game OverviewInput:1352Round 1:Even: min(1,3) = 1Odd: max(5,2) = 515Round 2:Even: min(1,5) = 11Final Result: 1
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)
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen