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 - In-Place Simulation INPUT nums array (length = 8) 1 3 5 2 4 8 2 2 0 1 2 3 4 5 6 7 Properties: - Length: 8 (power of 2) - Values: integers Pair Grouping [1,3] [5,2] [4,8] [2,2] Operation pattern: i=0: MIN, i=1: MAX i=2: MIN, i=3: MAX ALGORITHM STEPS 1 Round 1: n=8 to n=4 min(1,3)=1, max(5,2)=5 min(4,8)=4, max(2,2)=2 Result: [1,5,4,2] 2 Round 2: n=4 to n=2 min(1,5)=1, max(4,2)=4 Result: [1,4] 3 Round 3: n=2 to n=1 min(1,4)=1 Result: [1] 4 Check: n == 1 Process ends Return nums[0] Reduction Tree [1,3,5,2,4,8,2,2] [1,5,4,2] [1,4] [1] FINAL RESULT Last remaining number: 1 Output: 1 Status: OK Execution Trace 8 elements --> 4 elements 4 elements --> 2 elements 2 elements --> 1 element Iterations: log2(8) = 3 Key Insight: In-place simulation modifies the array directly, alternating between MIN and MAX operations based on index parity. For even indices (0, 2, 4...), use MIN of pairs. For odd indices (1, 3, 5...), use MAX of pairs. Time Complexity: O(n) since we process n + n/2 + n/4 + ... = 2n elements. Space: O(1) in-place. TutorialsPoint - Min Max Game | In-Place Simulation Approach
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