M is alone and he has an array . M wants to choose two integers such that and the value is maximum.
What is the maximum value M can get?
Example:
Input: n=4,arr = { 3, 4, 2, 3 }
Output: 3
Approach
Java
public class ANDChoice {public static void main(String[] args) {int n = 4;long[] arr = { 3, 4, 2, 3 };int ans = 0;for (int i = 31; i >= 0; --i) {int temp = ans | (1 << i);int cnt = 0;for (int j = 0; j < n; ++j) {if ((temp & arr[j]) == temp) {cnt++;}}if (cnt >= 2) {ans = ans | (1 << i);}}System.out.println(ans);}}
No comments:
Post a Comment