Problem solving with programming: Minimum AND of all subsets
Given array of size N, we have to find all the subsets of size > 2 of this array. Apply AND (bitwise &) on all the subsets. Print the minimum among those numbers.
For example let us consider the array {2, 8, 13, 7}, the minimum AND of any subset of size > 2 is 0 for subset {7,8}
Suppose that there exists some numbers whose AND is the minimum value among all the subsets. If we add any number of elements to this set, we get the same result. In the above example if add any element to {7,8} the result is no less than zero.
So the solution is to simply AND all the elements of the array!
Given array of size N, we have to find all the subsets of size > 2 of this array. Apply AND (bitwise &) on all the subsets. Print the minimum among those numbers.
For example let us consider the array {2, 8, 13, 7}, the minimum AND of any subset of size > 2 is 0 for subset {7,8}
Suppose that there exists some numbers whose AND is the minimum value among all the subsets. If we add any number of elements to this set, we get the same result. In the above example if add any element to {7,8} the result is no less than zero.
So the solution is to simply AND all the elements of the array!
Read full article from Problem solving with programming: Minimum AND of all subsetsScanner reader = new Scanner(System.in);int t = reader.nextInt();while ( t-- > 0 ){int n = reader.nextInt();long [] nums = new long[n];int i;long result = Long.MAX_VALUE;for( i = 0; i < n; i++ ){nums[i] = reader.nextLong();result = result & nums[i];}System.out.println(result);}