leetcodedaybyday - Beats 100% with C++ and Beats 69.68% with Python3
IntuitionThe problem involves repeatedly reducing the size of the array by selecting minimum or maximum values based on the index. At each step, adjacent elemen
# Intuition
The problem involves repeatedly reducing the size of the array by selecting minimum or maximum values based on the index. At each step, adjacent elements are grouped, and the result depends on whether the index is even (min) or odd (max). This process continues until a single element remains.

# Approach
1. **Iterative Reduction**:
   - Use a while loop to process the array while its size is greater than 1.
   - At each step:
     - Iterate through the first half of the array.
     - For even indices, replace the element with the minimum of the adjacent elements.
     - For odd indices, replace the element with the maximum of the adjacent elements.
   - Halve the size of the array at each step.

2. **Final Result**:
   - The last remaining element in the array is the result.

# Complexity
- **Time Complexity**:  
  - \(O(n)\), where \(n\) is the size of the input array. Each reduction step processes half the elements, and the total number of steps is \(O(\log n)\).
  - Summing all operations gives \(O(n)\).

- **Space Complexity**:  
  - \(O(1)\), as the transformation is performed in-place.
---
# Code
```cpp []
class Solution {
public:
    int minMaxGame(vector<int>& nums) {
        while (nums.size() > 1){
            vector<int> newNum;
            for (int i = 0; i < nums.size() / 2; i++){
                if (i % 2 == 0){
                    newNum.push_back(min(nums[2 * i], nums[2 * i + 1]));
                } else {
                    newNum.push_back(max(nums[2 * i], nums[2 * i + 1]));
                }
            }
            nums = newNum;
        }
        return nums[0];
    }
};



```
```python3 []
class Solution:
    def minMaxGame(self, nums: List[int]) -> int:
        n = len(nums)
        while n != 1:
            for i in range(int(n // 2)):
                if i % 2 == 0:
                    nums[i] = min(nums[ 2 * i], nums[2 * i + 1])
                else:
                    nums[i] = max(nums[ 2 * i], nums[2 * i + 1])
            n = n // 2
        return nums[0]



```


['C++', 'Python3']