Class Solution
- java.lang.Object
-
- g2301_2400.s2354_number_of_excellent_pairs.Solution
-
public class Solution extends Object
2354 - Number of Excellent Pairs.Hard
You are given a 0-indexed positive integer array
numsand a positive integerk.A pair of numbers
(num1, num2)is called excellent if the following conditions are satisfied:- Both the numbers
num1andnum2exist in the arraynums. - The sum of the number of set bits in
num1 OR num2andnum1 AND num2is greater than or equal tok, whereORis the bitwise OR operation andANDis the bitwise AND operation.
Return the number of distinct excellent pairs.
Two pairs
(a, b)and(c, d)are considered distinct if eithera != corb != d. For example,(1, 2)and(2, 1)are distinct.Note that a pair
(num1, num2)such thatnum1 == num2can also be excellent if you have at least one occurrence ofnum1in 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 <= 60
- Both the numbers
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcountExcellentPairs(int[] nums, int k)
-