Number of Excellent Pairs - Problem

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 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

Here's the key insight: For any two numbers a and b, the number of set bits in (a OR b) + the number of set bits in (a AND b) equals the number of set bits in a + the number of set bits in b. This transforms our problem from complex bitwise operations to simple bit counting!

Goal: Return the number of distinct excellent pairs.

Important notes:

  • Pairs (a,b) and (c,d) are distinct if either a != c or b != d
  • Pairs like (1,2) and (2,1) are considered different
  • A number can be paired with itself if it appears in the array

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,3,1], k = 3
โ€บ Output: 5
๐Ÿ’ก Note: Unique numbers are [1,2,3]. Check all pairs: (1,1): bits(1|1)+bits(1&1)=1+1=2<3 โœ—, (1,2): bits(1|2)+bits(1&2)=2+0=2<3 โœ—, (1,3): bits(1|3)+bits(1&3)=2+1=3โ‰ฅ3 โœ“, (2,1): 2+0=2<3 โœ—, (2,2): bits(2|2)+bits(2&2)=1+1=2<3 โœ—, (2,3): bits(2|3)+bits(2&3)=2+2=4โ‰ฅ3 โœ“, (3,1): 2+1=3โ‰ฅ3 โœ“, (3,2): 2+2=4โ‰ฅ3 โœ“, (3,3): bits(3|3)+bits(3&3)=2+2=4โ‰ฅ3 โœ“. Total: 5 pairs.
example_2.py โ€” All Pairs Excellent
$ Input: nums = [5,1,1], k = 1
โ€บ Output: 4
๐Ÿ’ก Note: Unique numbers are [5,1]. Since k=1 is low, most pairs work: (5,5): bits(5)=2, 2+2=4โ‰ฅ1 โœ“, (5,1): bits(5)+bits(1)=2+1=3โ‰ฅ1 โœ“, (1,5): same as (5,1) โœ“, (1,1): bits(1)=1, 1+1=2โ‰ฅ1 โœ“. All 4 pairs are excellent.
example_3.py โ€” High Threshold
$ Input: nums = [1,1,1,1], k = 10
โ€บ Output: 0
๐Ÿ’ก Note: Only unique number is 1. The pair (1,1) has bits(1|1)+bits(1&1)=1+1=2, which is less than k=10. No excellent pairs exist.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค 60
  • Each number has at most 20 bits (since 220 > 109)

Visualization

Tap to expand
The Bitwise Identity MagicExample: a=5 (101), b=3 (011)a | b = 111 (7) โ†’ 3 bitsa & b = 001 (1) โ†’ 1 bitSum: 3 + 1 = 4 bitsSame as: bits(5) + bits(3) = 2 + 2 = 4Frequency ApproachBit Count 1: freq=2 (nums: 1,2)Bit Count 2: freq=1 (nums: 3)Check: 1+2=3โ‰ฅk? Yes!Excellent pairs: 2ร—1ร—2 = 4(ร—2 for bidirectional pairs)Transform bitwise problem โ†’ arithmetic problem๐Ÿ’ก Key InsightInstead of O(nยฒ) pairwise OR/AND operations,use O(n) bit counting + O(400) frequency multiplicationTime complexity: O(nยฒ ร— log(max)) โ†’ O(n + 400)
Understanding the Visualization
1
The Key Identity
For any two numbers, OR captures all set bits, AND captures common bits, and their sum equals the total individual bits
2
Frequency Mapping
Instead of checking each pair, group numbers by bit count and use multiplication
3
Mathematical Optimization
For each pair of bit counts summing to โ‰ฅk, multiply their frequencies
Key Takeaway
๐ŸŽฏ Key Insight: The mathematical identity `bits(a|b) + bits(a&b) = bits(a) + bits(b)` transforms an expensive bitwise problem into simple frequency counting and arithmetic, reducing complexity from O(nยฒ) to O(n).
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
28.5K Views
Medium-High Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen