Maximum AND Value | Practice | GeeksforGeeks

Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arri, arrj) from the array such that i != j.
Note: AND is bitwise '&' operator. 

Example 1:
Input: 
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.

Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has 
Maximum AND Value 0.
0
vishnu12654 days ago
public static int maxAND (int arr[], int N) {
        int res = 0;
        int pattern = 0;
        for(int bit=31;bit>=0;bit--){
            int value = (1<<bit);
            if(value < 0){
                value = value * -1;
            }
            pattern = pattern + value ;
            
            int count = 0;
            for(int i=0;i<arr.length;i++){
                if((arr[i] & pattern) == pattern){
                    count++;
                }
            }
            if(count>=2){
                res = res + value;
            }
            else{
                pattern = pattern - value;
            }
        }
        return res;
    }
0
gujjulassr1 month ago
0.9/1.9
class Solution{     // Function for finding maximum AND value.   public static int maxAND (int arr[], int N) {              // Your code here       // You can add extra function (if required)       int max=0;       for(int i=0;i<arr.length;i++){           max=Math.max(max,arr[i]);       }       int sb=(int)((Math.log(max)/(Math.log(2))));       sb=(int)Math.pow(2,sb);       int res=0;       int count=0;       while(sb>=1){           for(int i=0;i<arr.length;i++){               if(res==0 && (sb&arr[i])==sb){                   count++;               }               else if(res!=0 && (sb&arr[i])==sb && (res&arr[i])==res){                   count++;               }           }                     if(count>=2){               res+=sb;           }           // System.out.println(res+" "+count);           count=0;           sb=sb/2;       }       return res;   }   }
+1
srujannwankhede7862 months ago
 
class Solution{   public static int maxAND (int arr[], int N) {       int pattern = 0;       int res = 0;       for(int i = 16; i>=0; i--){                     int count = 0;           for(int j =0; j<arr.length; j++){               if( (arr[j] & (1<<i)) == (1<<i) )                   count++;           }           if(count>=2){               pattern = check(pattern,(1<<i), arr);               res = pattern;           }       }       return res;   }      public static int check(int p,int incr, int[] arr){       int c = 0;       for(int j =0; j<arr.length; j++){           if((arr[j]&(p+incr)) == (p+incr) )               c++;       }       if(c>=2){           return (p+incr);       }       return p;          }}
-3
kaustubhug2 months ago
class Solution{
    
    public static int maxinarray(int arr[], int N)
    {
        int max = arr[0];
        for(int i = 1; i<N;i++)
        max = Math.max(max,arr[i]);
        
        return max;
    }
    
    public static int chkBits(int pattern, int arr[], int N)
    {
        int count  = 0;
        for(int i = 0; i<N; i++)
        {
            if((pattern & arr[i]) == pattern)
            count+=1;
        }
        return count;
    }
    
    public static int maxAND (int arr[], int N) {
        int max = maxinarray(arr,N); 
        int count = 0, res = 0;
        int m = (int)(Math.floor(Math.log(max)/Math.log(2)));
        for(int bits = m; bits >=0; bits--)
        {
            count = chkBits(res|(1<<bits),arr,N);
            if(count>=2)
            res |= (1<<bits);
        }
        return res;
    }
-1
geminicode3 months ago
Nlog(M) solution giving TLE in python !!!!
-1
kake13373 months ago
       int m=log2(*max_element(arr,arr+N));
       int r=0,p=0;
       while(m>=0)
       {
       p=p|(1<<m);
       int c=0;
       for(int i=0;i<N;i++)
       {
           if((arr[i]&p)==p)
           c++;
           if(c==2)
           break;
       }
       if(c==2)
       r=p;
       else
       p=r;
       m--;
       }
       return r;
+1
shubham211019973 months ago
int maxAND (int arr[], int n)   {       // Naive Approach       int max=0;       for(int i=0;i<n;i++)       {           for(int j=i+1;j<n;j++)           {                             int res= arr[i] & arr[j];               if(res>max)               max=res;                                           }       }
}
 
note:- time complexity is high and pass230 cases .Anyone can suggest how to optimize the code.
+1
chessnoobdj4 months ago
C++
int maxAND (int arr[], int N)
    {
        int mx = *max_element(arr, arr+N);
        int len = log2(mx)+1, ans = 0;
        for(int i=len; i>=0; i--){
            int curr = ans|(1<<i), cnt = 0;
            for(int j=0; j<N; j++){
                if((curr&arr[j]) == curr)
                    cnt += 1;
                if(cnt == 2)
                    break;
            }
            if(cnt == 2)
                ans = curr;
        }
        return ans;
    }
-2
anutiger4 months ago
    int helper(vector<int> &arr,int i,int ans){
        int cnt = 0;
        for(int j : arr){
            int chk1 = j & ans;
            int chk  = (1 << i) & j;
            if(chk1 == ans and chk != 0){
                cnt++;
            }
        }
        return cnt;
    }
    int maxAND (int arr[], int N)
    {
        int ans = 0;
        vector<int> res;
        for(int i = 0; i < N ; i ++) res.push_back(arr[i]);
        for(int i = 31; i >=0 ; i--){
            int temp = helper(res,i,ans);
            if(temp >= 2){
                ans = ans | (1 << i);
            }
        }
        return ans;
    }
0
kamaan551
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.