1557. XOR Mask Under Limit
A warehouse stores n crates, and crate i carries the tag value nums[i]. A scanner is configured with a mask x; it reports, for every crate, the value nums[i] ^ x (bitwise XOR), and the scanner's score is the sum of all reported values. The firmware only accepts masks with 0 <= x <= limit.
Return the highest score any permitted mask can achieve. If several masks tie, the score is the same, so only the score matters. The score can exceed 32 bits, so use 64-bit arithmetic. Trying every mask is far too slow when limit is large; aim for O(n log limit) time and O(log limit) extra space.
Example 1
- Input:
- nums = [5,9,12], limit = 6
- Output:
- 32
- Explanation:
Mask 2 turns the tags into 7, 11 and 14 for a total of 32, which beats every other mask from 0 to 6.
Example 2
- Input:
- nums = [7,7,7], limit = 0
- Output:
- 21
- Explanation:
Only mask 0 is allowed, so the score is simply 7 + 7 + 7 = 21.
Constraints
1 ≤ nums.length ≤ 105
0 ≤ nums[i], limit ≤ 230 - 1
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 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms
Expected complexity
- Time
- O(n log limit)
- Space
- O(log limit)