Problem · Array

Count Valid Bitwise Pairs

MediumStripeFULLTIMENEW GRADOA
See Stripe hiring insights

Stripe's analytics team is studying relationships between values stored in an array.

Given an array values of non-negative integers and a non-negative integer k, determine the number of pairs of indices (i, j) such that i < j and the source bitwise condition holds.

The source condition simplifies to:

values[i] + (values[i] XOR values[j]) = k

Return the total number of valid pairs.

Examples
01 · Example 1
values = [1,2,3]
k = 4
return = 1

The only valid pair is (1, 2) using 1-based positions: 1 + (1 XOR 2) = 1 + 3 = 4.

Constraints
  • 1 <= values.length <= 10^5
  • 0 <= k <= 10^18
  • 0 <= values[i] <= 10^18
  • The sum of values.length over all test cases does not exceed 3 * 10^5 in the original assessment format.
More Stripe problems
drafts saved locally
public long countValidBitwisePairs(long[] values, long k) {
  // write your code here
}
values[1,2,3]
k4
expected1
sign in to submit