Maximum AND Value coding solution


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.






STUDY EVERTHING




class Solution

{

    public:

    int checkBit(int pattern, int arr[], int n)

    {

    int count = 0;

    for(int i = 0; i<n; i++)

    if((pattern & arr[i]) == pattern)

    count++;

    return count;

}

    int maxAND (int arr[], int n)

    {

        int res = 0,  count;

        for(int bit = 16; bit >= 0; bit--)

        {

            count = checkBit(res | (1 << bit),arr,n);

            if (count >= 2)

            res |= (1<< bit);

        } return res;

    }

};





Click on the above button to know the much more details



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post