1543. Pairwise OR Parity
Two teams each submit a list of permission flags, stored as non-negative integers in the arrays a and b. An auditor combines every flag of the first team with every flag of the second team using bitwise OR, producing a.length * b.length merged values, and then folds all of those values together with bitwise XOR.
Return that final XOR, i.e. the XOR of a[i] | b[j] over all i and j. For example, a = [1, 2] and b = [4] merge into 5 and 6, whose XOR is 3.
Forming all pairs costs too much when both lists are long. Treat each bit position on its own: the bit is set in the result exactly when an odd number of pairs have it set after OR, and a pair lacks it only when both elements lack it. Count zeros per bit in each list and finish in O((n + m) * 30) time with O(1) extra space. The answer fits in a 32-bit integer.
Example 1
- Input:
- a = [1,2], b = [4]
- Output:
- 3
- Explanation:
The merged values are 1|4 = 5 and 2|4 = 6, and 5 ^ 6 = 3.
Example 2
- Input:
- a = [5,0], b = [2,1]
- Output:
- 1
- Explanation:
The merged values are 7, 5, 2 and 1, and their XOR is 1.
Constraints
1 ≤ a.length, b.length ≤ 105; 0 ≤ a[i], b[j] ≤ 230 - 1
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,400 msC++ 600 msJava 1,200 msJavaScript 1,200 msTypeScript 1,200 ms
Expected complexity
- Time
- O((n + m) log C)
- Space
- O(1)