Skip to main content
LeetCode 1829, Medium. Topics: Array, Bit Manipulation, Prefix Sum. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 19 parametrized pytest cases, and a playground notebook:

Problem

You are given a sorted array nums of n non-negative integers and an integer maximumBit. You want to perform the following query n times:
  • Find a non-negative integer k < 2^maximumBit such that nums[0] XOR nums[1] XOR … XOR nums[nums.length - 1] XOR k is maximized. k is the answer to the ith query.
  • Remove the last element from the current array nums.
Return an array answer, where answer[i] is the answer to the ith query.

Examples

Constraints

  • nums.length == n
  • 1 <= n <= 10^5
  • 1 <= maximumBit <= 20
  • 0 <= nums[i] < 2^maximumBit
  • nums is sorted in ascending order.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026