Count OR Pairs in Java

One array containing non-negative numbers is given to us. Also, a number K is given. Our task is to count the number of pairs from the given array such that the OR operation between the elements in the pair gives a value greater than K.

Example 1:

Input

int inArr1[] = {1, 3, 7, 9, 15, 20, 25}, K = 15

Output

11

Explanation:

The pairs that give a value more than 15 in OR operation are: (15, 20), (15, 25), (1, 20), (3, 20), (7, 20), (9, 20), (25, 20), (1, 25), (3, 25), (7, 25), (9, 25). The count of these pairs is 11. Hence the output is 11.

Example 2:

Input

int inArr1[] = {0, 1, 5, 8, 10, 19, 15, 25, 30, 40, 45}, K = 35

Output

19

Explanation:

The pairs that give a value more than 35 in OR operation are: (0, 40), (1, 40), (5, 40), (8, 40), (10, 40), (19, 40), (15, 40), (25, 40), (30, 40), (45, 40), (0, 45), (1, 45), (5, 45), (8, 45), (10, 45), (19, 45), (15, 45), (25, 45), (30, 45). The count of these pairs is 19. Hence the output is 19.

Simple Approach

The simple approach is to find all the pairs and then do the OR operation between the elements of the pair. Those pairs whose OR value is greater than K will be responsible for increasing the count by 1. The final value of the count is our answer.

Here is the algorithm:

Step 1: Create a variable 'cntORPairs' for keeping the number of pairs whose OR value is greater than K. Assign value 0 to it.

Step 2: Run a loop from 0 to 'len' (say, iterator = 'j')

Step 3: Run a nested loop from 'j' + 1 to 'len' (say, iterator = 'k')

Step 4: Check if the OR value of 'inputArr[j]' and 'inputArr[k]' is more than 'K' or not:

Step 5: Increment 'cntORPairs' by 1.

Step 6: Finally, return the 'cntORPairs'.

Now, observe the following program.

FileName: CountOrPair.java

Output:

```For the input array:
1 3 7 9 15 20 25
The total count of OR pairs whose value is greater than 15 is: 11

For the input array:
0 1 5 8 10 19 15 25 30 40 45
The total count of OR pairs whose value is greater than 35 is: 19
```

Complexity Analysis: The program is finding all of the pairs using the nested for-loops. There are a total of (N * (N - 1)) / 2 pairs. Therefore, the overall time complexity of the program is O(N2), where 'N' is the size of the input array. Also, the program is not using extra space, which makes the space complexity of the program O(1).

Mathematical Approach

The concept is to find the count of elements that are larger than 'K'. Elements larger than 'K' can form pairs from the rest of every other element of the input array inputArr[]. It is because if a number is greater than 'K', then its OR value with any of the elements will always be greater than 'K'.

Let's take an example for a better understanding:

Let the array be inputArr[] : {15, 1, 2, 3}

Let 'K' be : 7

The bit representation of 'K' is: 0111

Bit representation of 'inputArr[0]' i.e. 15 is : 1111

Bit representation of 'inputArr[1]' i.e. 1 is : 0001

OR value of 'inputArr[0]' and 'inputArr[1]' : 15 | 1 = 15, which is greater than 7. The OR value is greater than 7 because the first element 15 is greater than 7. If we take two elements that are less than 7 can never give a value greater than 7. Ex: inputArr[2] | inputArr[3] = 2 | 3 gives 3, which is less than 7.

Observe the following algorithm:

Step 1: Create a variable 'cntORPairs' for keeping the number of pairs. Assign value 0 to it.

Step 2: Create a variable (say, 'cntK') for keeping the elements larger than K and initialize it with the value 0.

Step 3: Run a loop from 0 to 'N' (say, loop variable = 'j')

Step 4: Check if 'inputArr[i]' is larger than 'K' :

• Increase 'cntK' by 1
• Increase 'cntORPairs' with ('N' - 'cntK') (We can form 'N' pairs with a number larger than 'K'. But we need to subtract 'cntK' every time while adding so that pairs are not repeated).

Step 5: Finally, return 'cntORPairs'.

The implementation of the above-mentioned steps is mentioned below.

FileName: CountOrPair1.java

Output:

```For the input array:
1 3 7 9 15 20 25
The total count of OR pairs whose value is greater than 15 is: 11

For the input array:
0 1 5 8 10 19 15 25 30 40 45
The total count of OR pairs whose value is greater than 35 is: 19
```

Complexity Analysis: The program using a single loop that makes the time complexity of the program O(N), where N is the total elements of the input array. The space complexity of the program is constant, i.e., O(1).