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 <= 60When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of numbers and want to find every possible pair where a special mathematical condition is met. The brute force approach simply examines every single possible pair of numbers available, one by one, to see if it satisfies the requirement.
Here's how the algorithm would work step-by-step:
def count_excellent_pairs(number_list, threshold):
distinct_numbers = list(set(number_list))
# We eliminate duplicates first to ensure we only process unique pairs exactly once
excellent_pairs_count = 0
for first_index in range(len(distinct_numbers)):
for second_index in range(len(distinct_numbers)):
first_value = distinct_numbers[first_index]
second_value = distinct_numbers[second_index]
bitwise_or_result = first_value | second_value
bitwise_and_result = first_value & second_value
or_set_bits = bin(bitwise_or_result).count('1')
and_set_bits = bin(bitwise_and_result).count('1')
# Bit manipulation mimics the set-bit counting requirement for the threshold check
if (or_set_bits + and_set_bits) >= threshold:
excellent_pairs_count += 1
# Return the accumulated total after exhaustively checking every single distinct pair
return excellent_pairs_count
The problem asks for the number of distinct pairs of numbers whose bitwise OR and bitwise AND have a combined set bit count of at least k. Using the bitwise identity, the sum of set bits in (a OR b) and (a AND b) is identical to the sum of set bits in a plus set bits in b. Consequently, the condition reduces to finding pairs of unique numbers whose individual popcounts add up to at least k.
Here's how the algorithm would work step-by-step:
def count_excellent_pairs(nums: list[int], k: int) -> int:
# Remove duplicate values since pairs must be distinct combinations of numbers
unique_numbers = set(nums)
# Count the set bits for each unique number in the array
bit_counts = sorted(number.bit_count() for number in unique_numbers)
total_pairs = 0
total_unique = len(bit_counts)
right_index = total_unique - 1
# Use a two-pointer approach to find pairs with sum of set bits at least k
for left_index in range(total_unique):
while right_index >= 0 and bit_counts[left_index] + bit_counts[right_index] >= k:
right_index -= 1
# All elements from right_index + 1 to the end form valid pairs
valid_count = total_unique - (right_index + 1)
total_pairs += valid_count
return total_pairs
| Case | How to Handle |
|---|---|
| Array contains duplicate numbers | Using a sorted unique elements array with frequency counting prevents duplicate pair generation and simplifies the index logic. |
| Array with a single element | The loop boundaries or pair constraints will naturally yield zero since at least two elements are required to form a pair. |
| Threshold is zero | All possible pairs will satisfy the condition since any pair has at least zero set bits, so the answer is combinations of n items taken two at a time plus n. |
| Threshold is larger than the maximum possible set bits | The algorithm will correctly compare the bit counts and return zero since no pair meets the excessively high threshold. |
| Maximum-sized inputs with up to 10 to the 5th elements and large values | Converting the problem to operate on the sum of set bits of individual numbers using bit manipulation reduces the time complexity to O(n log n) with sorting. |
| Values with identical bit counts | Using two pointers on a sorted array of set bit counts correctly aggregates all valid combinations in linear time after sorting. |
| Language-specific integer overflow during bit operations | Standard 32-bit or 64-bit integers easily accommodate the bit lengths up to 10 to the 9th without exceeding limits. |
| Self pairs where i equals j | The problem permits identical indices if the bit count of num AND num plus num OR num is at least the threshold, which simplifies to twice the set bits of num. |