Problem · Array

Dominating XOR Pairs

Learn this problem
MediumIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Given an array arr of positive integers, count the unordered index pairs (i, j) such that:

  • 0 ≤ i < j < arr.length
  • (arr[i] XOR arr[j]) > (arr[i] AND arr[j])

Return the number of qualifying pairs as a long.

Function

dominatingXorPairs(arr: int[]) → long

Examples

Example 1

arr = [4, 3, 5, 2]return = 4

The qualifying value pairs are (4, 3), (4, 2), (3, 5), and (5, 2). For each pair, the XOR value is greater than the AND value, so the result is 4.

Constraints

  • 1 ≤ arr.length ≤ 2 × 10^5
  • 1 ≤ arr[i] ≤ 10^9

More IBM problems

drafts saved locally
public long dominatingXorPairs(int[] arr) {
    // Write your code here
}
arr[4, 3, 5, 2]
expected4
checking account