Leetcode Problem 2354. Number of Excellent Pairs

2354. Number of Excellent Pairs

Leetcode Solutions

Counting Set Bits and Using Inclusion-Exclusion Principle

  1. Create a set to store unique numbers from the input array nums.
  2. Create an array countArray to store the count of numbers with a certain number of set bits.
  3. Iterate over the unique numbers and increment the count in countArray at the index equal to the number of set bits in the number.
  4. Initialize a variable count to store the number of excellent pairs.
  5. Use two nested loops to iterate over countArray and for each pair of indices (bit, bit_), if their sum is greater than or equal to k, increment count by countArray[bit] * countArray[bit_].
  6. Return the value of count.
UML Thumbnail

Brute Force with Set Bits Precomputation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...