Talentd
FleetCode

3215. Count Triplets with Even XOR Set Bits II

Medium62.0% acceptance1 companyPremium elsewhere, free here
Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits.

Example 1:

Input: a = [1], b = [2], c = [3]

Output: 1

Explanation:

The only triplet is (a[0], b[0], c[0]) and their XOR is: 1 XOR 2 XOR 3 = 002.

Example 2:

Input: a = [1,1], b = [2,3], c = [1,5]

Output: 4

Explanation:

Consider these four triplets:

  • (a[0], b[1], c[0]): 1 XOR 3 XOR 1 = 0112
  • (a[1], b[1], c[0]): 1 XOR 3 XOR 1 = 0112
  • (a[0], b[0], c[1]): 1 XOR 2 XOR 5 = 1102
  • (a[1], b[0], c[1]): 1 XOR 2 XOR 5 = 1102

Constraints:

  • 1 <= a.length, b.length, c.length <= 105
  • 0 <= a[i], b[i], c[i] <= 109

Asked at 1 company

Hints open one at a time

Hint 1
If x and y both have an even number of set bits, how many set bits do their XOR have?
Hint 2
If exactly one of x and y has an even number of set bits, how many set bits do their XOR have?
Hint 3
Conclude that if the XOR of three elements would have an even number of set bits, an even number of them (0 or 2) should have an odd number of set bits!
Hint 4
For each array count the number of elements that have even set bits and also count the ones that have odd set bits.
11:31
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[1]
[2]
[3]
Input
[1,1]
[2,3]
[1,5]
Your code is saved on this device.Solution