Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int n = sc.nextInt();
- int arr[] = new int[n];
- for (int i = 0; i < n; i++) {
- arr[i] = sc.nextInt();
- }
- System.out.println(maximumBitwiseAnd(n, arr));
- }
- public static int maximumBitwiseAnd(int n, int[] arr) {
- int[] prefix = new int[n];
- int[] suffix = new int[n];
- prefix[0] = (1<<30) - 1;
- suffix[n-1] = prefix[0];
- for(int i=1; i < n; i++){
- prefix[i] = prefix[i-1]&arr[i-1];
- }
- for(int i=n-2; i >= 0; i--){
- suffix[i] = suffix[i+1]&arr[i+1];
- }
- int res = 0;
- for(int i=0; i < n; i++){
- res = Math.max(res, prefix[i]&suffix[i]);
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement