Skip to content

Commit 713ef68

Browse files
committed
1803. Count Pairs With XOR in a Range: RETRY
1 parent 9ed02ff commit 713ef68

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,3 +1359,4 @@ mod s1799_maximize_score_after_n_operations;
13591359
mod s1800_maximum_ascending_subarray_sum;
13601360
mod s1801_number_of_orders_in_the_backlog;
13611361
mod s1802_maximum_value_at_a_given_index_in_a_bounded_array;
1362+
mod s1803_count_pairs_with_xor_in_a_range;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* [1803] Count Pairs With XOR in a Range
3+
*
4+
* Given a (0-indexed) integer array nums and two integers low and high, return the number of nice pairs.
5+
*
6+
* A nice pair is a pair (i, j) where 0 <= i < j < nums.length and low <= (nums[i] XOR nums[j]) <= high.
7+
*
8+
*
9+
* Example 1:
10+
*
11+
*
12+
* Input: nums = [1,4,2,7], low = 2, high = 6
13+
* Output: 6
14+
* Explanation: All nice pairs (i, j) are as follows:
15+
* - (0, 1): nums[0] XOR nums[1] = 5
16+
* - (0, 2): nums[0] XOR nums[2] = 3
17+
* - (0, 3): nums[0] XOR nums[3] = 6
18+
* - (1, 2): nums[1] XOR nums[2] = 6
19+
* - (1, 3): nums[1] XOR nums[3] = 3
20+
* - (2, 3): nums[2] XOR nums[3] = 5
21+
*
22+
*
23+
* Example 2:
24+
*
25+
*
26+
* Input: nums = [9,8,4,2,1], low = 5, high = 14
27+
* Output: 8
28+
* Explanation: All nice pairs (i, j) are as follows:
29+
* ​​​​​ - (0, 2): nums[0] XOR nums[2] = 13
30+
* - (0, 3): nums[0] XOR nums[3] = 11
31+
* - (0, 4): nums[0] XOR nums[4] = 8
32+
* - (1, 2): nums[1] XOR nums[2] = 12
33+
* - (1, 3): nums[1] XOR nums[3] = 10
34+
* - (1, 4): nums[1] XOR nums[4] = 9
35+
* - (2, 3): nums[2] XOR nums[3] = 6
36+
* - (2, 4): nums[2] XOR nums[4] = 5
37+
*
38+
*
39+
* Constraints:
40+
*
41+
*
42+
* 1 <= nums.length <= 2 * 10^4
43+
* 1 <= nums[i] <= 2 * 10^4
44+
* 1 <= low <= high <= 2 * 10^4
45+
*
46+
*/
47+
pub struct Solution {}
48+
49+
// problem: https://leetcode.com/problems/count-pairs-with-xor-in-a-range/
50+
// discuss: https://leetcode.com/problems/count-pairs-with-xor-in-a-range/discuss/?currentPage=1&orderBy=most_votes&query=
51+
52+
// submission codes start here
53+
54+
impl Solution {
55+
pub fn count_pairs(nums: Vec<i32>, low: i32, high: i32) -> i32 {
56+
0
57+
}
58+
}
59+
60+
// submission codes end
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
#[ignore]
68+
fn test_1803_example_1() {
69+
let nums = vec![1, 4, 2, 7];
70+
let low = 2;
71+
let high = 6;
72+
73+
let result = 6;
74+
75+
assert_eq!(Solution::count_pairs(nums, low, high), result);
76+
}
77+
78+
#[test]
79+
#[ignore]
80+
fn test_1803_example_1() {
81+
let nums = vec![1, 4, 2, 7];
82+
let low = 2;
83+
let high = 6;
84+
85+
let result = 6;
86+
87+
assert_eq!(Solution::count_pairs(nums, low, high), result);
88+
}
89+
}

0 commit comments

Comments
 (0)