Count Pairs With AND Greater Than or Equal to XOR
You are given an integer n and an array nums of length n containing positive integers.
Count the number of pairs (i, j) such that:
- 0 <= i < j < n
- (nums[i] & nums[j]) >= (nums[i] ^ nums[j])
Here:
- & denotes the bitwise AND operation
- ^ denotes the bitwise XOR operation
Return the total number of such pairs.
Example 1:
Input: nums = [1, 2, 3, 4, 5]
Output: 2
Explanation:
The valid pairs are:
- (2, 3) because 2 & 3 = 2 and 2 ^ 3 = 1
- (4, 5) because 4 & 5 = 4 and 4 ^ 5 = 1
So, the answer is 2.
Example 2:
Input: nums = [8, 1, 2, 15, 10]
Output: 3
Explanation:
The valid pairs are:
- (8, 15) because 8 & 15 = 8 and 8 ^ 15 = 7
- (8, 10) because 8 & 10 = 8 and 8 ^ 10 = 2
- (15, 10) because 15 & 10 = 10 and 15 ^ 10 = 5
So, the answer is 3.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [3, 5, 6, 7]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == nums.length
- 1 <= n <= 105
- 1 <= nums[i] <= 109