|
| 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