Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
Input: nums = [1,4,2,7], low = 2, high = 6
Output: 6
Explanation: All nice pairs (i, j) are as follows:
- (0, 1): nums[0] XOR nums[1] = 5
- (0, 2): nums[0] XOR nums[2] = 3
- (0, 3): nums[0] XOR nums[3] = 6
- (1, 2): nums[1] XOR nums[2] = 6
- (1, 3): nums[1] XOR nums[3] = 3
- (2, 3): nums[2] XOR nums[3] = 5
Example 2:
Input: nums = [9,8,4,2,1], low = 5, high = 14 Output: 8 Explanation: All nice pairs (i, j) are as follows: - (0, 2): nums[0] XOR nums[2] = 13 - (0, 3): nums[0] XOR nums[3] = 11 - (0, 4): nums[0] XOR nums[4] = 8 - (1, 2): nums[1] XOR nums[2] = 12 - (1, 3): nums[1] XOR nums[3] = 10 - (1, 4): nums[1] XOR nums[4] = 9 - (2, 3): nums[2] XOR nums[3] = 6 - (2, 4): nums[2] XOR nums[4] = 5
Constraints:
1 <= nums.length <= 2 * 1041 <= nums[i] <= 2 * 1041 <= low <= high <= 2 * 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation · Trie
[1,4,2,7] 2 6
[9,8,4,2,1] 5 14
count-paths-with-the-given-xor-value)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1803: Count Pairs With XOR in a Range
class Trie {
private Trie[] children = new Trie[2];
private int cnt;
public void insert(int x) {
Trie node = this;
for (int i = 15; i >= 0; --i) {
int v = (x >> i) & 1;
if (node.children[v] == null) {
node.children[v] = new Trie();
}
node = node.children[v];
++node.cnt;
}
}
public int search(int x, int limit) {
Trie node = this;
int ans = 0;
for (int i = 15; i >= 0 && node != null; --i) {
int v = (x >> i) & 1;
if (((limit >> i) & 1) == 1) {
if (node.children[v] != null) {
ans += node.children[v].cnt;
}
node = node.children[v ^ 1];
} else {
node = node.children[v];
}
}
return ans;
}
}
class Solution {
public int countPairs(int[] nums, int low, int high) {
Trie trie = new Trie();
int ans = 0;
for (int x : nums) {
ans += trie.search(x, high + 1) - trie.search(x, low);
trie.insert(x);
}
return ans;
}
}
// Accepted solution for LeetCode #1803: Count Pairs With XOR in a Range
type Trie struct {
children [2]*Trie
cnt int
}
func newTrie() *Trie {
return &Trie{}
}
func (this *Trie) insert(x int) {
node := this
for i := 15; i >= 0; i-- {
v := (x >> i) & 1
if node.children[v] == nil {
node.children[v] = newTrie()
}
node = node.children[v]
node.cnt++
}
}
func (this *Trie) search(x, limit int) (ans int) {
node := this
for i := 15; i >= 0 && node != nil; i-- {
v := (x >> i) & 1
if (limit >> i & 1) == 1 {
if node.children[v] != nil {
ans += node.children[v].cnt
}
node = node.children[v^1]
} else {
node = node.children[v]
}
}
return
}
func countPairs(nums []int, low int, high int) (ans int) {
tree := newTrie()
for _, x := range nums {
ans += tree.search(x, high+1) - tree.search(x, low)
tree.insert(x)
}
return
}
# Accepted solution for LeetCode #1803: Count Pairs With XOR in a Range
class Trie:
def __init__(self):
self.children = [None] * 2
self.cnt = 0
def insert(self, x):
node = self
for i in range(15, -1, -1):
v = x >> i & 1
if node.children[v] is None:
node.children[v] = Trie()
node = node.children[v]
node.cnt += 1
def search(self, x, limit):
node = self
ans = 0
for i in range(15, -1, -1):
if node is None:
return ans
v = x >> i & 1
if limit >> i & 1:
if node.children[v]:
ans += node.children[v].cnt
node = node.children[v ^ 1]
else:
node = node.children[v]
return ans
class Solution:
def countPairs(self, nums: List[int], low: int, high: int) -> int:
ans = 0
tree = Trie()
for x in nums:
ans += tree.search(x, high + 1) - tree.search(x, low)
tree.insert(x)
return ans
// Accepted solution for LeetCode #1803: Count Pairs With XOR in a Range
/**
* [1803] 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:
*
*
* Input: nums = [1,4,2,7], low = 2, high = 6
* Output: 6
* Explanation: All nice pairs (i, j) are as follows:
* - (0, 1): nums[0] XOR nums[1] = 5
* - (0, 2): nums[0] XOR nums[2] = 3
* - (0, 3): nums[0] XOR nums[3] = 6
* - (1, 2): nums[1] XOR nums[2] = 6
* - (1, 3): nums[1] XOR nums[3] = 3
* - (2, 3): nums[2] XOR nums[3] = 5
*
*
* Example 2:
*
*
* Input: nums = [9,8,4,2,1], low = 5, high = 14
* Output: 8
* Explanation: All nice pairs (i, j) are as follows:
* - (0, 2): nums[0] XOR nums[2] = 13
* - (0, 3): nums[0] XOR nums[3] = 11
* - (0, 4): nums[0] XOR nums[4] = 8
* - (1, 2): nums[1] XOR nums[2] = 12
* - (1, 3): nums[1] XOR nums[3] = 10
* - (1, 4): nums[1] XOR nums[4] = 9
* - (2, 3): nums[2] XOR nums[3] = 6
* - (2, 4): nums[2] XOR nums[4] = 5
*
*
* Constraints:
*
*
* 1 <= nums.length <= 2 * 10^4
* 1 <= nums[i] <= 2 * 10^4
* 1 <= low <= high <= 2 * 10^4
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-pairs-with-xor-in-a-range/
// discuss: https://leetcode.com/problems/count-pairs-with-xor-in-a-range/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_pairs(nums: Vec<i32>, low: i32, high: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1803_example_1() {
let nums = vec![1, 4, 2, 7];
let low = 2;
let high = 6;
let result = 6;
assert_eq!(Solution::count_pairs(nums, low, high), result);
}
#[test]
#[ignore]
fn test_1803_example_2() {
let nums = vec![9, 8, 4, 2, 1];
let low = 5;
let high = 14;
let result = 8;
assert_eq!(Solution::count_pairs(nums, low, high), result);
}
}
// Accepted solution for LeetCode #1803: Count Pairs With XOR in a Range
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1803: Count Pairs With XOR in a Range
// class Trie {
// private Trie[] children = new Trie[2];
// private int cnt;
//
// public void insert(int x) {
// Trie node = this;
// for (int i = 15; i >= 0; --i) {
// int v = (x >> i) & 1;
// if (node.children[v] == null) {
// node.children[v] = new Trie();
// }
// node = node.children[v];
// ++node.cnt;
// }
// }
//
// public int search(int x, int limit) {
// Trie node = this;
// int ans = 0;
// for (int i = 15; i >= 0 && node != null; --i) {
// int v = (x >> i) & 1;
// if (((limit >> i) & 1) == 1) {
// if (node.children[v] != null) {
// ans += node.children[v].cnt;
// }
// node = node.children[v ^ 1];
// } else {
// node = node.children[v];
// }
// }
// return ans;
// }
// }
//
// class Solution {
// public int countPairs(int[] nums, int low, int high) {
// Trie trie = new Trie();
// int ans = 0;
// for (int x : nums) {
// ans += trie.search(x, high + 1) - trie.search(x, low);
// trie.insert(x);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.