Count Pairs With XOR in a Range

Given a (0-indexed) integer array nums and two integers low and high, return the number of nice pairs.

A nice pair is a pair (i, j) where 0 <= i < j < nums.length and low <= (nums[i] XOR nums[j]) <= high.

Example 1
Inputnums = [1,4,2,7], low = 2, high = 6
Output6
All 6 pairs have XOR values within the range [2, 6].
Example 2
Inputnums = [9,8,4,2,1], low = 5, high = 14
Output8
There are 8 pairs whose XOR values are between 5 and 14 inclusive.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • 1 <= nums[i] <= 2 * 10^4
  • 1 <= low <= high <= 2 * 10^4

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate