Input : arr[] = {5, 1, 3, 4, 2}
Output : 2
7 is the maximum value possible of OR,
5|2 = 7 and 5|3 = 7
The first line contains a single integer ‘T’ representing the number of test cases.Then ‘T’ test cases follow.
The first line of each test case contains the integer ‘N’ representing the size of the input ‘ARR’.
The next line of each test case contains ‘N’ single space-separated integers that represent the elements of the ‘ARR’.
For each test case, return an integer denoting the size of the smallest subset with maximum possible OR.
Output for each test case will be printed in a separate line.
You don’t need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^4
0 <= ARR[i] <= 10^3
Time Limit: 1 sec
The maximum possible OR of any subset is the OR of the whole array. So let the OR of the whole array be equal to ‘K’.
The idea is to generate all possible subsets and check if any of them OR up to ‘K’. This can be done through recursion.
Here is the algorithm:
subsetOR(N , ARR):
helper(ARR, N, K, curr):
Return min(‘X’, ‘Y’).
The maximum possible OR of any subset is the OR of the whole array. So let the OR of the whole array be equal to ‘K’.
The idea is to generate all possible subsets and check if any of them OR up to ‘K’. Along with that, we will use memoization to store previously calculated results to avoid repetition and hence improving the time complexity.
Here is the algorithm:
subsetOR(N , ARR):
helper(ARR, N, K, MEMO):
The maximum possible OR of any subset is the OR of the whole array. So let the OR of the whole array be equal to ‘K’.
The idea is to use dynamic programming to generate all the possible subsets and check if these subsets OR up to ‘K’.
Here is the algorithm: