Min Max Game
EasyPrompt
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.
Example 1:
Input: nums = [1,3,5,2,4,8,2,2]
Output: 1
Explanation: The following arrays are the results of applying the algorithm repeatedly.
First: nums = [1,5,4,2]
Second: nums = [1,4]
Third: nums = [1]
1 is the last remaining number, so we return 1.Example 2:
Input: nums = [3]
Output: 3
Explanation: 3 is already the last remaining number, so we return 3.
Constraints:
1 <= nums.length <= 10241 <= nums[i] <= 109nums.lengthis a power of2.
Approaches
2 approaches with complexity analysis and trade-offs.
This approach directly simulates the process described in the problem statement. In each step of the algorithm, we create a new array to store the results of the min/max operations. We repeat this process, halving the array size each time, until only one element remains.
Algorithm
- Initialize a variable
nwith the length ofnums. - If
nis 1, returnnums[0]. - Create a copy of the input array, let's call it
currentNums. - Start a
whileloop that continues as long asn > 1. - Inside the loop, update
nton / 2. - Create a new integer array
newNumsof sizen. - Loop from
i = 0ton - 1:- If
iis even, setnewNums[i] = min(currentNums[2 * i], currentNums[2 * i + 1]). - If
iis odd, setnewNums[i] = max(currentNums[2 * i], currentNums[2 * i + 1]).
- If
- After the inner loop, assign
newNumstocurrentNums. - When the
whileloop finishes,currentNumswill contain a single element. ReturncurrentNums[0].
Walkthrough
We start a loop that continues as long as the length of the array nums is greater than 1. Inside the loop, we first get the current length n. We create a new temporary array, newNums, with a size of n / 2. We then iterate from i = 0 to n / 2 - 1. For each i, if i is even, we calculate min(nums[2 * i], nums[2 * i + 1]) and store it in newNums[i]. If i is odd, we calculate max(nums[2 * i], nums[2 * i + 1]) and store it in newNums[i]. After filling newNums, we replace the original nums array with newNums. The loop continues with the new, smaller nums array. Once the loop terminates (when nums has only one element), we return that single element, nums[0].
import java.util.Arrays; class Solution { public int minMaxGame(int[] nums) { int n = nums.length; if (n == 1) { return nums[0]; } int[] currentNums = Arrays.copyOf(nums, n); while (n > 1) { n /= 2; int[] newNums = new int[n]; for (int i = 0; i < n; i++) { if (i % 2 == 0) { newNums[i] = Math.min(currentNums[2 * i], currentNums[2 * i + 1]); } else { newNums[i] = Math.max(currentNums[2 * i], currentNums[2 * i + 1]); } } currentNums = newNums; } return currentNums[0]; }}Complexity
Time
O(N), where `N` is the initial length of `nums`. The total number of operations is the sum of the lengths of the arrays at each step: `N/2 + N/4 + ... + 1`, which is a geometric series that sums to `N - 1`. Thus, the complexity is linear in the size of the input array.
Space
O(N). In the first step, we create a new array of size `N/2`. In subsequent steps, we create smaller arrays, but the space is dominated by the largest temporary array. We also make an initial copy of `nums`, which takes `O(N)` space.
Trade-offs
Pros
Simple to understand and implement as it directly follows the problem description.
Does not modify the original input array (if we make a copy first).
Cons
Uses extra space to store the intermediate arrays, which can be significant for a very large input array.
Solutions
Solution
class Solution { public int minMaxGame ( int [] nums ) { for ( int n = nums . length ; n > 1 ;) { n >>= 1 ; for ( int i = 0 ; i < n ; ++ i ) { int a = nums [ i << 1 ], b = nums [ i << 1 | 1 ]; nums [ i ] = i % 2 == 0 ? Math . min ( a , b ) : Math . max ( a , b ); } } return nums [ 0 ]; } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.