You are given an array consisting of integer values. Find an integer such that the value of the following function is minimized:
- , represents a bitwise XOR operation
If multiple such exist, then print the minimum possible value of .
Example:
Input: N=4, a[]={1,4,4,4}
Output: 4
Approach
Java
public class XORValue {public static void main(String args[]) {int N = 4;int a[] = { 1, 4, 4, 4 };int[] countbit = new int[64];for (int i = 0; i < N; i++) {long v = a[i];int idx = 0;while (v != 0) {countbit[idx] = countbit[idx] + (int) (v % 2);v = v / 2;idx++;}}long val = 0;for (int i = 63; i >= 0; i--) {val = 2 * val;if (2 * countbit[i] > N) {val = val + 1;}}System.out.println(val);}}
No comments:
Post a Comment