Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
0
Indexable
Never
//if there is atleast 1 set bit in position i int the numbers ,then the resultant will have a set bit at 
//i th position.

class Solution {
public:
    vector<int> bin(int n,int size){
        vector<int>ans;
        while(n>0){
            int l=n%2;
            ans.push_back(l);
            n/=2;;
        }
        while(ans.size()<size) ans.push_back(0);
        reverse(ans.begin(),ans.end());
        return ans;
    }

    int maximumXOR(vector<int>& arr) {
        sort(arr.begin(),arr.end());
        int n=arr.size();
        int last=arr[n-1];
        int count=0;
        while(last>0){
            count++;
            last/=2;
        }
        vector<int>binlast=bin(last,count);
        vector<int>res(binlast.size());

        for(int i=0;i<n;i++){
            vector<int>tmp=bin(arr[i],binlast.size());
            for(int i=tmp.size()-1;i>=0;i--){
                if(tmp[i]==1) res[i]=1;
            }
        }

        int fin=0;
        int pd=1;
        for(int i=res.size()-1;i>=0;i--){
           // cout<<res[i]<<" ";
            fin+=res[i]*pd;
            pd*=2; 
        } 
        return fin; 
    }
};
Leave a Comment