Solving Leetcode Interviews in Seconds with AI: Number of Excellent Pairs
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2354" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 <= 105 1 <= nums[i] <= 109 1 <= k <= 60
Explanation
Here's the breakdown of the approach and the Python code:
- Key Idea: The number of set bits in
num1 OR num2+ the number of set bits innum1 AND num2is always equal to the number of set bits innum1+ the number of set bits innum2. Therefore, we only need to count the set bits in each number and check if their sum is greater than or equal tok. Optimization: We can precompute the number of set bits for each unique number in the array. Then, we can iterate through all possible pairs of unique numbers, count the pairs where the sum of set bits is greater than or equal to
k, and multiply by the frequency of each number to account for duplicates. Instead of iterating through all pairs, we can count the frequency of set bits and find the pairs using that count in O(n) time.Complexity:
- Runtime: O(N), where N is the number of elements in
nums. - Space: O(M), where M is the number of distinct elements in
nums.
- Runtime: O(N), where N is the number of elements in
Code
def countExcellentPairs(nums, k):
"""
Counts the number of distinct excellent pairs in the given array.
Args:
nums: A list of positive integers.
k: A positive integer.
Returns:
The number of distinct excellent pairs.
"""
distinct_nums = set(nums)
bit_counts = [0] * 32 # Maximum 32 bits in a 32-bit integer
for num in distinct_nums:
count = bin(num).count('1')
bit_counts[count] += 1
total_pairs = 0
for i in range(1, 32):
if bit_counts[i] > 0:
for j in range(1, 32):
if bit_counts[j] > 0:
if i + j >= k:
if i == j:
total_pairs += bit_counts[i] * (bit_counts[i])
else:
total_pairs += bit_counts[i] * bit_counts[j]
return total_pairs