Watch 8 video solutions for Final Element After Subarray Deletions, a medium level problem involving Array, Math, Brainteaser. This walkthrough by Sanyam IIT Guwahati has 1,769 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums.
Two players, Alice and Bob, play a game in turns, with Alice playing first.
nums[l..r] such that r - l + 1 < m, where m is the current length of the array.Alice aims to maximize the final element, while Bob aims to minimize it. Assuming both play optimally, return the value of the final remaining element.
Example 1:
Input: nums = [1,5,2]
Output: 2
Explanation:
One valid optimal strategy:
[1], array becomes [5, 2].[5], array becomes [2]. Thus, the answer is 2.Example 2:
Input: nums = [3,7]
Output: 7
Explanation:
Alice removes [3], leaving the array [7]. Since Bob cannot play a turn now, the answer is 7.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem Overview: You are given an array and repeatedly delete subarrays until only one element remains. The task is to determine which value must remain at the end regardless of how the deletions are performed.
Approach 1: Simulation of All Deletions (Exponential Time)
A direct way to think about the problem is to simulate every possible subarray deletion and track the remaining arrays until a single element remains. At each step you choose a subarray, remove it, and continue recursively. This explores a huge state space because the number of possible deletion sequences grows exponentially with the array length. The approach requires copying arrays and exploring branches, resulting in O(2^n) or worse time and large recursion overhead. Space complexity can reach O(2^n) due to recursion and stored states. This approach is mainly useful for understanding the mechanics on very small arrays.
Approach 2: Mathematical / Brain Teaser Insight (O(n))
The key observation is that subarray deletions do not change the parity contribution of elements when the process is reduced to its invariant. Regardless of the order of deletions, elements effectively combine through an associative cancellation property similar to XOR. When two segments merge after a deletion, their cumulative effect behaves exactly like combining values with the XOR operation.
This means the entire sequence of deletions can be viewed as repeatedly collapsing segments until only one value remains. Because XOR is associative and commutative, the final result is independent of the order of operations. The remaining value is simply the XOR of all elements in the array.
Implementation becomes straightforward: iterate through the array once and maintain a running XOR. Each element updates the accumulator using result ^= nums[i]. After processing all elements, the accumulator represents the final value that must remain after any valid sequence of subarray deletions. The algorithm runs in O(n) time and uses O(1) additional space.
This pattern appears frequently in array problems where operations reduce segments repeatedly. Recognizing invariants like XOR or parity is a common math trick and often shows up in game theory style interview puzzles.
Recommended for interviews: Interviewers expect the mathematical insight rather than brute force exploration. Explaining the invariant and reducing the process to a single XOR pass demonstrates strong problem‑solving skills. Mentioning the naive simulation first shows you understand the process, but the O(n) brain teaser solution is the one that matters.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Simulation of Deletions | O(2^n) | O(2^n) | Only for understanding the mechanics on very small arrays |
| Mathematical XOR Invariant (Brain Teaser) | O(n) | O(1) | Optimal solution expected in interviews and competitive programming |