Skip to content

Commit e4d2f2f

Browse files
committed
2354. Number of Excellent Pairs: RETRY
1 parent e91b134 commit e4d2f2f

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,3 +1784,4 @@ mod s2350_shortest_impossible_sequence_of_rolls;
17841784
mod s2351_first_letter_to_appear_twice;
17851785
mod s2352_equal_row_and_column_pairs;
17861786
mod s2353_design_a_food_rating_system;
1787+
mod s2354_number_of_excellent_pairs;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* [2354] Number of Excellent Pairs
3+
*
4+
* You are given a 0-indexed positive integer array nums and a positive integer k.
5+
* A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied:
6+
*
7+
* Both the numbers num1 and num2 exist in the array nums.
8+
* 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.
9+
*
10+
* Return the number of distinct excellent pairs.
11+
* 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.
12+
* 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.
13+
*
14+
* Example 1:
15+
*
16+
* Input: nums = [1,2,3,1], k = 3
17+
* Output: 5
18+
* Explanation: The excellent pairs are the following:
19+
* - (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.
20+
* - (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.
21+
* - (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.
22+
* So the number of excellent pairs is 5.
23+
* Example 2:
24+
*
25+
* Input: nums = [5,1,1], k = 10
26+
* Output: 0
27+
* Explanation: There are no excellent pairs for this array.
28+
*
29+
*
30+
* Constraints:
31+
*
32+
* 1 <= nums.length <= 10^5
33+
* 1 <= nums[i] <= 10^9
34+
* 1 <= k <= 60
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/number-of-excellent-pairs/
40+
// discuss: https://leetcode.com/problems/number-of-excellent-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn count_excellent_pairs(nums: Vec<i32>, k: i32) -> i64 {
46+
0
47+
}
48+
}
49+
50+
// submission codes end
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
#[ignore]
58+
fn test_2354_example_1() {
59+
let nums = vec![1, 2, 3, 1];
60+
let k = 3;
61+
62+
let result = 5;
63+
64+
assert_eq!(Solution::count_excellent_pairs(nums, k), result);
65+
}
66+
67+
#[test]
68+
#[ignore]
69+
fn test_2354_example_2() {
70+
let nums = vec![5, 1, 1];
71+
let k = 10;
72+
73+
let result = 0;
74+
75+
assert_eq!(Solution::count_excellent_pairs(nums, k), result);
76+
}
77+
}

0 commit comments

Comments
 (0)