↓Skip to main content
  1. LeetCode/

LeetCode 3828: Final Element After Subarray Deletions

·2 mins· ·
LeetCode Medium Math Brainteaser Game-Theory Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-01

  • Submissions: Accepted (first try)

Approach
#

A move may delete any subarray of length < m (the current length), so one move can leave a single element, as long as it is the first or the last (delete a subarray of length m - 1).

  • Lower bound: Alice’s first move leaves only nums[0] or nums[-1], whichever is larger, ending the game. The answer is at least max(first, last).
  • Upper bound: if Alice’s first move does not end the game, she deleted one contiguous subarray and cannot remove both ends, so the new array keeps at least one original end. Bob then deletes down to the smaller of the new array’s ends, which is <= that original end <= max(first, last).

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

My Solution
#

class Solution:
    def finalElement(self, nums: List[int]) -> int:
        return max(nums[0], nums[-1])

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Simulating the whole game: once “one move can leave an end” is seen, it is constant time.

Interview Explanation
#

Every move can delete down to just the first or last element. Alice does that immediately and gets the larger end; if she does not, she only removed one block, so an original end survives, and Bob deletes down to the smaller of the new ends, which cannot exceed the original larger end. So the answer is max(first, last).

Related

LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 1040: Moving Stones Until Consecutive II
·3 mins
LeetCode Medium Sliding-Window Math Sorting Array
LeetCode 1040 note: max moves = total gaps minus the edge gap the first move wastes; min moves uses a length-n window holding the most stones, with a special case of 2 when n - 1 stones are already consecutive and one is far away
LeetCode 3649: Number of Perfect Pairs
·2 mins
LeetCode Medium Two-Pointers Math Sorting Array
LeetCode 3649 note: simplify the condition to “larger absolute value <= 2 × smaller absolute value”, then count with two pointers over sorted absolute values
LeetCode 2745: Construct the Longest New String
·2 mins
LeetCode Medium Dynamic-Programming Math Greedy Brainteaser
LeetCode 2745 note: AA and BB must alternate, using min(x, y) pairs plus one extra of the more common kind, and every AB fits in
LeetCode 3618: Split Array by Prime Indices
·2 mins
LeetCode Medium Math Number-Theory Sieve Array
LeetCode 3618 note: primality is about the index; a sieve of Eratosthenes marks all of 0..n-1 at once, faster than trial division per index
LeetCode 292: Nim Game
·1 min
LeetCode Easy Math Brainteaser Minimax-Algorithm Game-Theory
LeetCode 292 note: whoever faces a multiple of 4 stones loses, so the first player wins when n % 4 != 0