https://www.geeksforgeeks.org/find-triplets-in-an-array-whose-and-is-maximum/
Given an array of positive integers of size n. Find the count of the triplets whose AND is maximum and also find that maximum given that i < j < k where i, j, k are the indices of the numbers.
Assuming that numbers will not be greater than 10^9.
Assuming that numbers will not be greater than 10^9.
Examples:
Input : a[] = {1, 2, 3, 4, 5, 6}
Output : 1 4
Explanation: Maximum number that can formed is 4 ( 4 & 5 & 6 ) and only 1 triplet is possible.
A naive approach is to use 3 loops and generate all triplets and calculate the maximum number that can be formed and count of such triplets.
Time Complexity: O(N^3)
Time Complexity: O(N^3)
A better approach is to first represent the number in its binary representation and store it in a 2d array. Since the number cannot be greater than 2^32 ( due to the constraints given in the question ), thus it will take at max 32 iterations for each number. We will also take boolean flag array which will represent which all numbers can be used to make the max triplet. Initially we set the array to true since every number can be used.
Let the maximum AND number be X initially zero.
Now we want to maximize X so we start traversing the 2D table from the index which represent 32th bit of the number and we will count the number of 1’s which are present at the 32th bit of the numbers which are available for the triplets ie whose flags are true. If the count of 1’s is greater than equal to 3 that means there is/are triplets possible to make the ith bit of X set, then we will set the flag of all the numbers whose ith bit is not set and also add the power i to the base 2 to X. Else, if the count is less than 3 then ith of X will be unset and we do not need to change the flags of the numbers since there can be combinations of 1’s and 0’s for that bit.
We will repeat the above process for every bit in reverse order that is from 32 till 0th.
At the we will count the number of numbers whose flags are set let that be r. Then for number of triplets we just need to calculate rC3 { r*(r-1)*(r-2)/6 }.
Let the maximum AND number be X initially zero.
Now we want to maximize X so we start traversing the 2D table from the index which represent 32th bit of the number and we will count the number of 1’s which are present at the 32th bit of the numbers which are available for the triplets ie whose flags are true. If the count of 1’s is greater than equal to 3 that means there is/are triplets possible to make the ith bit of X set, then we will set the flag of all the numbers whose ith bit is not set and also add the power i to the base 2 to X. Else, if the count is less than 3 then ith of X will be unset and we do not need to change the flags of the numbers since there can be combinations of 1’s and 0’s for that bit.
We will repeat the above process for every bit in reverse order that is from 32 till 0th.
At the we will count the number of numbers whose flags are set let that be r. Then for number of triplets we just need to calculate rC3 { r*(r-1)*(r-2)/6 }.
Time Complexity: O(NlogN)
Since each number is can be converted to its binary in logN.
Since each number is can be converted to its binary in logN.