You are given an array consisting of N integers. You need to find the number of k-element subsequences of the given array where the bitwise AND of the subsequence's elements is maximal. Also, find the maximal AND value.
Example:
Let the array be [1, 3, 6, 7] and K=3. The possible k-element subsequences of the given array are: {1, 3, 6}, {1, 3, 7}, {1, 6, 7}, {3, 6, 7}. Applying AND operation on all possible subsequences we get values: 0, 1, 0, 2 respectively. The maximal AND value of these subsequences is 2, and only 1 subsequence {3, 6, 7} has this value.
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains two space-separated integers ‘N’ and ‘K’ denoting the number of elements present in the array and the length of the subsequences.
The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.
Output format:
For each test case, two space-separated integers are printed denoting the maximal AND value and the number of subsequences having maximal AND value, respectively.
Output for each test case is printed on a separate line.
Note:
The number of possible k-element subsequences having that maximal AND value can be very large. So, print the answer modulo 1000000007.
You do not need to print anything, it has already been taken care of. Just return an array of size two where the 1st element is max AND value and 2nd element is the number of subsequences having this maximal AND value.
1 <= T <= 10
2 <= N <= 5 * 10^4
2 <= K <= N
0 <= Arr[i] <= 10^8
Where ‘T’ represents the number of test cases, ‘N’ represents the number of elements present in the array and ‘K’ represents the length of the subsequences.
Time Limit: 1 sec
2
4 3
1 3 6 7
3 2
9 9 9
2 1
9 3
For the first test case refer to the example explained above.
For the second test case we have, array: [9, 9, 9], N = 3 and K = 2. The possible k-element subsequences of the given array are: {9, 9}, {9, 9}, {9, 9}. Applying AND operation on all possible subsequences we get values: 9, 9, 9 respectively. The maximal AND value of these subsequences is 9, and all subsequences have this value.
2
5 2
1 2 3 4 5
4 4
6 3 7 0
4 1
0 1
Can you solve this problem by generating all the subsequences?
O(M), where M is the number of k-element subsequences of the array (M = N! / ((N-K)! * K!)).
The number of k-element subsequences of the given array of length N is, M = N! / ((N-K)! * K!). As we are generating all these subsequences. Hence, the overall time complexity is O(M).
O(K), where K is the length of the subsequences.
Extra space is required for the recursion stack. The maximum depth of recursion is O(K). Hence, the overall space complexity is O(K).