1558. Longest XOR Above K

Hard7.5 points estimatedTriePrefix SumBit Manipulation

A sensor logs n readings in order, stored in nums. Any run of consecutive readings (a window) has a "signature": the bitwise XOR of all readings in it. An alert threshold k is configured, and a window is loud when its signature is greater than or equal to k.

Return the length of the longest loud window. If no window is loud, return 0. Windows must be non-empty, and when several windows share the maximum length only the length is reported. Testing every window is O(n^2) and too slow at the upper limit; aim for O(n log M) time and O(n log M) space, where M = 2^16 bounds every value.

Example 1

Input:
nums = [3,1,6,2], k = 6
Output:
4
Explanation:

The window [3,1,6,2] has XOR 6, which is at least 6, so the whole array of length 4 qualifies.

Example 2

Input:
nums = [1,1,1], k = 2
Output:
0
Explanation:

No window has XOR above 1, so there is no loud window and the answer is 0.

Constraints

1 ≤ nums.length ≤ 105

0 ≤ nums[i], k < 216

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,400 msC++ 600 msJava 1,200 msJavaScript 1,200 msTypeScript 1,200 ms

Expected complexity

Time
O(n log M)
Space
O(n log M)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…