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.
You are given a 0-indexed positive integer array nums and a positive integer k.
A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied:
num1 and num2 exist in the array nums.num1 OR num2 and num1 AND num2 is greater than or equal to k, where OR is the bitwise OR operation and AND is the bitwise AND operation.Return the number of distinct excellent pairs.
Two pairs (a, b) and (c, d) are considered distinct if either a != c or b != d. For example, (1, 2) and (2, 1) are distinct.
Note that a pair (num1, num2) such that num1 == num2 can also be excellent if you have at least one occurrence of num1 in the array.
Example 1:
Input: nums = [1,2,3,1], k = 3 Output: 5 Explanation: The excellent pairs are the following: - (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3. - (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3. - (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3. So the number of excellent pairs is 5.
Example 2:
Input: nums = [5,1,1], k = 10 Output: 0 Explanation: There are no excellent pairs for this array.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 60Problem summary: You are given a 0-indexed positive integer array nums and a positive integer k. A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied: Both the numbers num1 and num2 exist in the array nums. The sum of the number of set bits in num1 OR num2 and num1 AND num2 is greater than or equal to k, where OR is the bitwise OR operation and AND is the bitwise AND operation. Return the number of distinct excellent pairs. Two pairs (a, b) and (c, d) are considered distinct if either a != c or b != d. For example, (1, 2) and (2, 1) are distinct. Note that a pair (num1, num2) such that num1 == num2 can also be excellent if you have at least one occurrence of num1 in the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Binary Search · Bit Manipulation
[1,2,3,1] 3
[5,1,1] 10
two-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2354: Number of Excellent Pairs
class Solution {
public long countExcellentPairs(int[] nums, int k) {
Set<Integer> s = new HashSet<>();
for (int v : nums) {
s.add(v);
}
long ans = 0;
int[] cnt = new int[32];
for (int v : s) {
int t = Integer.bitCount(v);
++cnt[t];
}
for (int v : s) {
int t = Integer.bitCount(v);
for (int i = 0; i < 32; ++i) {
if (t + i >= k) {
ans += cnt[i];
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2354: Number of Excellent Pairs
func countExcellentPairs(nums []int, k int) int64 {
s := map[int]bool{}
for _, v := range nums {
s[v] = true
}
cnt := make([]int, 32)
for v := range s {
t := bits.OnesCount(uint(v))
cnt[t]++
}
ans := 0
for v := range s {
t := bits.OnesCount(uint(v))
for i, x := range cnt {
if t+i >= k {
ans += x
}
}
}
return int64(ans)
}
# Accepted solution for LeetCode #2354: Number of Excellent Pairs
class Solution:
def countExcellentPairs(self, nums: List[int], k: int) -> int:
s = set(nums)
ans = 0
cnt = Counter()
for v in s:
cnt[v.bit_count()] += 1
for v in s:
t = v.bit_count()
for i, x in cnt.items():
if t + i >= k:
ans += x
return ans
// Accepted solution for LeetCode #2354: Number of Excellent Pairs
/**
* [2354] Number of Excellent Pairs
*
* You are given a 0-indexed positive integer array nums and a positive integer k.
* A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied:
*
* Both the numbers num1 and num2 exist in the array nums.
* The sum of the number of set bits in num1 OR num2 and num1 AND num2 is greater than or equal to k, where OR is the bitwise OR operation and AND is the bitwise AND operation.
*
* Return the number of distinct excellent pairs.
* Two pairs (a, b) and (c, d) are considered distinct if either a != c or b != d. For example, (1, 2) and (2, 1) are distinct.
* Note that a pair (num1, num2) such that num1 == num2 can also be excellent if you have at least one occurrence of num1 in the array.
*
* Example 1:
*
* Input: nums = [1,2,3,1], k = 3
* Output: 5
* Explanation: The excellent pairs are the following:
* - (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
* - (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
* - (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
* So the number of excellent pairs is 5.
* Example 2:
*
* Input: nums = [5,1,1], k = 10
* Output: 0
* Explanation: There are no excellent pairs for this array.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^9
* 1 <= k <= 60
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-excellent-pairs/
// discuss: https://leetcode.com/problems/number-of-excellent-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_excellent_pairs(nums: Vec<i32>, k: i32) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2354_example_1() {
let nums = vec![1, 2, 3, 1];
let k = 3;
let result = 5;
assert_eq!(Solution::count_excellent_pairs(nums, k), result);
}
#[test]
#[ignore]
fn test_2354_example_2() {
let nums = vec![5, 1, 1];
let k = 10;
let result = 0;
assert_eq!(Solution::count_excellent_pairs(nums, k), result);
}
}
// Accepted solution for LeetCode #2354: Number of Excellent Pairs
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2354: Number of Excellent Pairs
// class Solution {
// public long countExcellentPairs(int[] nums, int k) {
// Set<Integer> s = new HashSet<>();
// for (int v : nums) {
// s.add(v);
// }
// long ans = 0;
// int[] cnt = new int[32];
// for (int v : s) {
// int t = Integer.bitCount(v);
// ++cnt[t];
// }
// for (int v : s) {
// int t = Integer.bitCount(v);
// for (int i = 0; i < 32; ++i) {
// if (t + i >= k) {
// ans += cnt[i];
// }
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.