You are given a 0-indexed array nums
of positive integers.
You need to choose three distinct indices i
, j
, and k
such that the number of set bits (i.e., the number of ones in the binary representation) in nums[i] XOR nums[j] XOR nums[k]
is even.
Return the number of ways to choose such three indices.
Example 1:
Input: nums = [2,1,3,0] Output: 4 Explanation: The following are the possible choices of i, j, and k: - (i,j,k) = (0,1,2). nums[i] XOR nums[j] XOR nums[k] = 2 XOR 1 XOR 3 = 0. The number of set bits in 0 is 0, which is even. - (i,j,k) = (0,1,3). nums[i] XOR nums[j] XOR nums[k] = 2 XOR 1 XOR 0 = 3. The number of set bits in 3 is 2, which is even. - (i,j,k) = (0,2,3). nums[i] XOR nums[j] XOR nums[k] = 2 XOR 3 XOR 0 = 1. The number of set bits in 1 is 1, which is odd. - (i,j,k) = (1,2,3). nums[i] XOR nums[j] XOR nums[k] = 1 XOR 3 XOR 0 = 2. The number of set bits in 2 is 1, which is odd. Thus, the number of ways is 2. Note that (i,j,k) = (0,3,1) is not a valid choice because it is not in increasing order.
Example 2:
Input: nums = [1,2,3] Output: 0 Explanation: The following are the possible choices of i, j, and k: - (i,j,k) = (0,1,2). nums[i] XOR nums[j] XOR nums[k] = 1 XOR 2 XOR 3 = 0. The number of set bits in 0 is 0, which is even. Thus, the number of ways is 1.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 108
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method explores all possible combinations of three numbers from the given set. For each combination, we check if it meets our specific criteria. Finally, we count all combinations which passed the check.
Here's how the algorithm would work step-by-step:
def count_triplets_with_even_xor_set_bits_brute_force(numbers):
numbers_length = len(numbers)
triplets_count = 0
for first_index in range(numbers_length):
for second_index in range(first_index + 1, numbers_length):
for third_index in range(second_index + 1, numbers_length):
# Calculate XOR of the three numbers
xor_result = numbers[first_index] ^ numbers[second_index] ^ numbers[third_index]
set_bits_count = 0
temp_xor_result = xor_result
# Count the set bits in the XOR result
while temp_xor_result > 0:
temp_xor_result &= (temp_xor_result - 1)
set_bits_count += 1
# Check if the number of set bits is even
if set_bits_count % 2 == 0:
# Increment the triplets count if even
triplets_count += 1
return triplets_count
The goal is to efficiently count specific groups of three numbers. The key is to recognize that for the condition to be met, there's a direct relationship between the number of set bits in the numbers, allowing us to derive one number from the others.
Here's how the algorithm would work step-by-step:
def count_triplets(numbers):
number_of_elements = len(numbers)
count = 0
set_bit_counts = [bin(number).count('1') for number in numbers]
for first_index in range(number_of_elements):
for second_index in range(first_index + 1, number_of_elements):
# Calculate XOR of the two numbers.
xor_result = numbers[first_index] ^ numbers[second_index]
# We need to find the parity of the XOR result.
xor_set_bits = bin(xor_result).count('1')
valid_third_numbers = 0
# Count numbers with matching parity.
for third_index in range(number_of_elements):
if third_index != first_index and third_index != second_index:
if set_bit_counts[third_index] % 2 == xor_set_bits % 2:
valid_third_numbers += 1
# Accumulate triplets found.
count += valid_third_numbers
# Each triplet was counted 6 times so adjust result
return count // 3
Case | How to Handle |
---|---|
Empty input array | Return 0 as no triplets can be formed. |
Array with fewer than 3 elements | Return 0 as a triplet requires at least 3 elements. |
Array with all elements being 0 | The XOR sum will always be 0, so the number of triplets is n choose 3, which can be calculated directly. |
Array with all elements having an even number of set bits | The XOR sum of any combination will always have an even number of set bits; calculate n choose 3. |
Array with all elements having an odd number of set bits | The XOR sum of any combination will always have an odd number of set bits; return 0. |
Array with MAX_INT values which cause integer overflow in the XOR calculation | Use 64-bit integers (long) or bitset to prevent overflow during the XOR operation. |
Input array with maximum size allowed | Ensure algorithm time complexity is better than O(n^3) to avoid time limit exceeded. |
Array containing negative numbers (if applicable based on constraints) | Handle negative numbers in set bit counting if the problem explicitly allows it. |