Min Max Game solution and explanation
9 more videos on this problem
Complete Weekly Contest 296 | Leetcode 2293 2294 2295 2296 |Partition Array Maximum Difference Is KCoding Decoded · 33:04 · 1K views
2293. Min Max Game (Leetcode Easy)Programming Live with Larry · 4:25 · 1K views
Leetcode 2293. Min Max Game | HindiThinkCode · 2:23 · 1K views
2293 Min Max Game | Zero to FAANG | Weekly 296 | Leetcode | Striver SDE Sheet | Shapnesh | WE2Programmers Zone · 18:39 · 701 views
LeetCode 2293 | Min Max Game | Array | JavaSleepy Cracker · 4:35 · 341 views
Min Max Game - LeetCode 2293 - Java Solutionalgobest - Computer Science · 4:45 · 222 views
LeetCode Weekly Contest 296 | Min Max Game | Python 3Bayu Aji · 5:30 · 194 views
[No Talking-Only Coding] Min-Max Game Algorithm @EasyAlgorithm ( Problem Solved) RAMM ProgrammingRAMM Prog · 9:44 · 180 views
LeetCode 2293 min max game[CE] Code Engineering · 7:35 · 117 views
Lecture#14: LeetCode - Min Max Game Question| DSA for Placementcodegram · 20:08 · 94 views
The 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.
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.
Approach overview
Problem Overview: You are given an array nums whose length is a power of two. Repeatedly reduce the array size by half. For each pair of elements, compute either min or max depending on the index: even indices take the minimum, odd indices take the maximum. Continue until only one number remains.
This is primarily a array reduction problem driven by rules. Each round constructs a smaller array derived from the previous one. The key observation: every iteration halves the array size, so the total amount of work across all rounds remains linear.
Approach 1: Iterative Simulation (Time: O(n), Space: O(1) or O(n))
Simulate the process exactly as described. While the array length is greater than 1, build the next round of values by iterating through pairs (nums[2*i], nums[2*i+1]). If i is even, store min(nums[2*i], nums[2*i+1]). If i is odd, store max(nums[2*i], nums[2*i+1]). Replace the current array with the newly computed one and repeat.
The insight is that although there are multiple rounds, the total number of operations across all rounds forms a geometric series: n + n/2 + n/4 + .... This sums to O(n). The implementation is straightforward and maps directly to the rules of the problem, making it the most practical solution. Space usage can be O(n) if a new array is created each round, or O(1) extra if the same array is reused.
Approach 2: Recursive Reduction (Time: O(n), Space: O(log n))
The same reduction logic can be expressed recursively. Create a helper function that receives the current array. If the array length is 1, return the element. Otherwise, construct the next reduced array using the same alternating min/max rule and recursively process it.
This approach models the game structure naturally: each recursive call represents one round of the game. Because the array size halves every time, recursion depth is log₂(n). The total number of element operations across all calls is still O(n). Recursion mainly improves conceptual clarity rather than performance.
Both approaches rely on simple iteration and pairwise comparisons, which makes this a classic simulation problem with heavy use of array indexing.
Recommended for interviews: The iterative simulation approach. Interviewers expect you to directly translate the rules into code and recognize that repeated halving keeps the total complexity at O(n). After presenting the iterative version, mentioning the recursive alternative shows a deeper understanding of the problem structure.
Approach 1: Iterative Approach
This approach involves iteratively reducing the array size by applying the min-max game rules until only one element remains. We use a while loop to repeatedly create a new array with half the size of the original array, filling it with minimum or maximum values as prescribed, until only one element is left.
def minMaxGame(nums):
while len(nums) > 1:
nums = [
min(nums[2 * i], nums[2 * i + 1]) if i % 2 == 0 else max(nums[2 * i], nums[2 * i + 1])
for i in range(len(nums) // 2)
]
return nums[0]
# Example usage
print(minMaxGame([1, 3, 5, 2, 4, 8, 2, 2]))import java.util.Arrays;
public class MinMaxGame {
public static int minMaxGame(int[] nums) {
while (nums.length > 1) {
int[] newNums = new int[nums.length / 2];
for (int i = 0; i < newNums.length; i++) {
if (i % 2 == 0) {
newNums[i] = Math.min(nums[2 * i], nums[2 * i + 1]);
} else {
newNums[i] = Math.max(nums[2 * i], nums[2 * i + 1]);
}
}
nums = newNums;
}
return nums[0];
}
public static void main(String[] args) {
int[] nums = {1,3,5,2,4,8,2,2};
System.out.println(minMaxGame(nums));
}
}#include <vector>
#include <iostream>
using namespace std;
int minMaxGame(vector<int>& nums) {
while (nums.size() > 1) {
vector<int> newNums(nums.size() / 2);
for (int i = 0; i < newNums.size(); i++) {
if (i % 2 == 0) {
newNums[i] = min(nums[2 * i], nums[2 * i + 1]);
} else {
newNums[i] = max(nums[2 * i], nums[2 * i + 1]);
}
}
nums = newNums;
}
return nums[0];
}
int main() {
vector<int> nums = {1,3,5,2,4,8,2,2};
cout << minMaxGame(nums) << endl;
return 0;
}function minMaxGame(nums) {
while (nums.length > 1) {
let newNums = [];
for (let i = 0; i < nums.length / 2; i++) {
if (i % 2 === 0) {
newNums.push(Math.min(nums[2 * i], nums[2 * i + 1]));
} else {
newNums.push(Math.max(nums[2 * i], nums[2 * i + 1]));
}
}
nums = newNums;
}
return nums[0];
}
// Example usage
console.log(minMaxGame([1, 3, 5, 2, 4, 8, 2, 2]));#include <stdio.h>
int minMaxGame(int* nums, int numsSize) {
while (numsSize > 1) {
int newSize = numsSize / 2;
for (int i = 0; i < newSize; ++i) {
if (i % 2 == 0) {
nums[i] = nums[2 * i] < nums[2 * i + 1] ? nums[2 * i] : nums[2 * i + 1];
} else {
nums[i] = nums[2 * i] > nums[2 * i + 1] ? nums[2 * i] : nums[2 * i + 1];
}
}
numsSize = newSize;
}
return nums[0];
}
int main() {
int nums[] = {1,3,5,2,4,8,2,2};
int result = minMaxGame(nums, 8);
printf("%d\n", result);
return 0;
}using System;
class MinMaxGame {
public static int MinMaxGame(int[] nums) {
while (nums.Length > 1) {
int newSize = nums.Length / 2;
int[] newNums = new int[newSize];
for (int i = 0; i < newSize; i++) {
if (i % 2 == 0) {
newNums[i] = Math.Min(nums[2 * i], nums[2 * i + 1]);
} else {
newNums[i] = Math.Max(nums[2 * i], nums[2 * i + 1]);
}
}
nums = newNums;
}
return nums[0];
}
static void Main(string[] args) {
int[] nums = {1,3,5,2,4,8,2,2};
Console.WriteLine(MinMaxGame(nums));
}
}This Python solution enlists list comprehension to construct the new array ‘newNums’. It simplifies each iteration by handling even and odd indexed elements accordingly until a single integer remains.
Time Complexity: O(n log n), due to successively halving the length of the array and processing each level.
Space Complexity: O(n) owing to the creation of a new half-sized list in each step.
Approach 2: Recursive Approach
This approach is based on recursively applying the min-max game until the array reduces to a single element. We define a recursive function that constructs the new array for each level of recursion and recurs until the base case of array size 1 is reached.
def minMaxRecursive(nums):
if len(nums) == 1:
return nums[0]
newNums = [
min(nums[2 * i], nums[2 * i + 1]) if i % 2 == 0 else max(nums[2 * i], nums[2 * i + 1])
for i in range(len(nums) // 2)
]
return minMaxRecursive(newNums)
# Example usage
print(minMaxRecursive([1, 3, 5, 2, 4, 8, 2, 2]))public class MinMaxGame {
public static int minMaxRecursive(int[] nums) {
if (nums.length == 1) return nums[0];
int[] newNums = new int[nums.length / 2];
for (int i = 0; i < newNums.length; i++) {
if (i % 2 == 0) {
newNums[i] = Math.min(nums[2 * i], nums[2 * i + 1]);
} else {
newNums[i] = Math.max(nums[2 * i], nums[2 * i + 1]);
}
}
return minMaxRecursive(newNums);
}
public static void main(String[] args) {
int[] nums = {1,3,5,2,4,8,2,2};
System.out.println(minMaxRecursive(nums));
}
}#include <vector>
#include <iostream>
using namespace std;
int minMaxRecursive(vector<int>& nums) {
int n = nums.size();
if (n == 1) return nums[0];
vector<int> newNums(n / 2);
for (int i = 0; i < n / 2; i++) {
if (i % 2 == 0) {
newNums[i] = min(nums[2 * i], nums[2 * i + 1]);
} else {
newNums[i] = max(nums[2 * i], nums[2 * i + 1]);
}
}
return minMaxRecursive(newNums);
}
int main() {
vector<int> nums = {1,3,5,2,4,8,2,2};
cout << minMaxRecursive(nums) << endl;
return 0;
}function minMaxRecursive(nums) {
if (nums.length === 1) return nums[0];
const newNums = [];
for (let i = 0; i < nums.length / 2; i++) {
if (i % 2 === 0) {
newNums.push(Math.min(nums[2 * i], nums[2 * i + 1]));
} else {
newNums.push(Math.max(nums[2 * i], nums[2 * i + 1]));
}
}
return minMaxRecursive(newNums);
}
// Example usage
console.log(minMaxRecursive([1, 3, 5, 2, 4, 8, 2, 2]));#include <stdio.h>
int minMaxRecursive(int* nums, int numsSize) {
if (numsSize == 1) return nums[0];
int newNums[numsSize / 2];
for (int i = 0; i < numsSize / 2; ++i) {
if (i % 2 == 0) {
newNums[i] = nums[2 * i] < nums[2 * i + 1] ? nums[2 * i] : nums[2 * i + 1];
} else {
newNums[i] = nums[2 * i] > nums[2 * i + 1] ? nums[2 * i] : nums[2 * i + 1];
}
}
return minMaxRecursive(newNums, numsSize / 2);
}
int main() {
int nums[] = {1,3,5,2,4,8,2,2};
printf("%d\n", minMaxRecursive(nums, 8));
return 0;
}using System;
class MinMaxGame {
public static int MinMaxRecursive(int[] nums) {
if (nums.Length == 1) return nums[0];
int newSize = nums.Length / 2;
int[] newNums = new int[newSize];
for (int i = 0; i < newSize; i++) {
if (i % 2 == 0) {
newNums[i] = Math.Min(nums[2 * i], nums[2 * i + 1]);
} else {
newNums[i] = Math.Max(nums[2 * i], nums[2 * i + 1]);
}
}
return MinMaxRecursive(newNums);
}
static void Main(string[] args) {
int[] nums = {1,3,5,2,4,8,2,2};
Console.WriteLine(MinMaxRecursive(nums));
}
}This Python solution employs a recursive function which checks a base condition of array length and updates recursively. The results are stored in a new list generated each time the function is called.
Time Complexity: O(n log n), due to recursive function calls reducing problem size.
Space Complexity: O(n log n), caused by stack frames holding function calls.
Approach 3: Simulation
According to the problem statement, we can simulate the entire process, and the remaining number will be the answer. In implementation, we do not need to create an additional array; we can directly operate on the original array.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
function minMaxGame(nums: number[]): number {
for (let n = nums.length; n > 1; ) {
n >>= 1;
for (let i = 0; i < n; ++i) {
const a = nums[i << 1];
const b = nums[(i << 1) | 1];
nums[i] = i % 2 == 0 ? Math.min(a, b) : Math.max(a, b);
}
}
return nums[0];
}func minMaxGame(nums []int) int {
for n := len(nums); n > 1; {
n >>= 1
for i := 0; i < n; i++ {
a, b := nums[i<<1], nums[i<<1|1]
if i%2 == 0 {
nums[i] = min(a, b)
} else {
nums[i] = max(a, b)
}
}
}
return nums[0]
}impl Solution {
pub fn min_max_game(mut nums: Vec<i32>) -> i32 {
let mut n = nums.len();
while n != 1 {
n >>= 1;
for i in 0..n {
nums[i] = (if (i & 1) == 1 { i32::max } else { i32::min })(
nums[i << 1],
nums[(i << 1) | 1],
);
}
}
nums[0]
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n log n), due to successively halving the length of the array and processing each level. |
| Recursive Approach | Time Complexity: O(n log n), due to recursive function calls reducing problem size. |
| Simulation | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Simulation | O(n) | O(1) extra or O(n) | Best general solution. Directly simulates the game rules and is easiest to implement in interviews. |
| Recursive Reduction | O(n) | O(log n) | Useful when modeling the problem as repeated rounds or practicing recursion-based solutions. |
Frequently asked questions
Is Min Max Game easy or hard?
Min Max Game is classified as an Easy problem. The challenge is mostly careful implementation of the alternating min/max rule while shrinking the array each round. Once the simulation pattern is recognized, the solution becomes straightforward.
How to solve Min Max Game in O(n)?
Iterate through the array in rounds. For each index i in the new array, compute min(nums[2*i], nums[2*i+1]) if i is even, otherwise compute max(nums[2*i], nums[2*i+1]). Replace the array with the reduced one and repeat until only one element remains. Because the array halves each round, the total runtime is linear.
What is the best approach for Min Max Game?
The iterative simulation approach is the best choice. You repeatedly build a new array where each element is either the min or max of a pair from the previous array. Because the array size halves each round, the total number of operations across all rounds is O(n) with constant or linear extra space depending on implementation.
What data structure is used in Min Max Game?
The problem primarily uses arrays. Each round reads values from the current array and writes results into a smaller array. No advanced data structures are required—only indexing, pair comparisons, and simple iteration.
What is the time complexity of Min Max Game?
The overall time complexity is O(n). Each round processes the current array once and reduces its size by half. The total work across rounds forms the series n + n/2 + n/4 + ... which sums to less than 2n.
Min Max Game Python or Java solution approach?
Both Python and Java implementations follow the same logic: iterate through pairs of elements, apply min for even indices and max for odd indices, and build the next reduced array. Continue until the array size becomes one. The algorithm runs in O(n) time in both languages.
Is Min Max Game asked at Google, Amazon, or Meta?
Min Max Game is an easy-level array simulation problem commonly used in coding interviews and practice platforms like LeetCode. Problems with iterative reduction and array simulation appear frequently in interviews at companies like Amazon and Google to evaluate basic problem-solving and implementation accuracy.
