Skip to main content

Final Element After Subarray Deletions - Solution & Explanation

MediumArrayMathBrainteaserGame Theory6 min readAsked at: Amazon
Practice this problem

Problem Statement

You are given an integer array nums.

Two players, Alice and Bob, play a game in turns, with Alice playing first.

  • In each turn, the current player chooses any subarray nums[l..r] such that r - l + 1 < m, where m is the current length of the array.
  • The selected subarray is removed, and the remaining elements are concatenated to form the new array.
  • The game continues until only one element remains.

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:

  • Alice removes [1], array becomes [5, 2].
  • Bob removes [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 <= 105
  • 1 <= nums[i] <= 105

Approach Overview

Problem 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.

Solution

Since Alice goes first, Alice can choose to remove all elements except the first and last elements, so the answer is at least max(nums[0], nums[n - 1]).

For the cases of elements at indices 1, 2, ..., n-2 (the middle elements), even if Alice wants to keep any of these middle elements, Bob can choose to remove it, so the answer is at most max(nums[0], nums[n - 1]).

Therefore, the answer is exactly max(nums[0], nums[n - 1]).

The time complexity is O(1) and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive Simulation of DeletionsO(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

Video Solution

Final Element After Subarray Deletions | LeetCode 3828 | Weekly Contest 487 • Sanyam IIT Guwahati • 1,852 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Final Element After Subarray Deletions easy or hard?
The problem is rated Medium because the operations appear complex at first. Once the XOR invariant is recognized, the implementation becomes straightforward with a single O(n) pass.
Final Element After Subarray Deletions Python/Java solution
The implementation is identical across languages: iterate through the array and XOR each element into an accumulator variable. Python, Java, C++, Go, and TypeScript all support the XOR operator, making the solution concise and efficient.
How to solve Final Element After Subarray Deletions in O(n)?
Iterate through the array and maintain a variable called result. For each element perform result ^= nums[i]. After processing all elements, result contains the value that must remain after any sequence of valid subarray deletions. The algorithm uses one linear pass and constant extra space.
What is the best approach for Final Element After Subarray Deletions?
The optimal approach uses a mathematical invariant: the final element equals the XOR of all numbers in the array. Subarray deletions effectively collapse segments, and because XOR is associative and commutative, the final value is independent of deletion order. A single pass computing the cumulative XOR solves the problem in O(n) time and O(1) space.
Is Final Element After Subarray Deletions asked at Google/Amazon/Meta?
Problems based on XOR invariants and array reduction frequently appear in interviews at companies like Google, Amazon, and Meta. Even if the exact question is different, recognizing associative operations and invariants is a common interview pattern.
What data structure is used in Final Element After Subarray Deletions?
The solution primarily uses a simple array traversal with a running XOR accumulator. No additional data structures such as stacks or hash maps are required, which keeps the space complexity constant.
What is the time complexity of Final Element After Subarray Deletions?
The optimal brain‑teaser solution runs in O(n) time because you scan the array once and compute a running XOR. Only a constant amount of extra memory is required, giving O(1) space complexity.

Ready to solve this problem?

Practice Final Element After Subarray Deletions with our built-in code editor and test cases.

Practice on FleetCode