Untitled
unknown
plain_text
2 years ago
655 B
5
Indexable
class Solution {
public:
int minOrAfterOperations(vector<int>& nums, int k) {
int n=nums.size();
int ans=0;
for(int j=30;j>=0;j--){
int cnt=0;//no of bits with 0 at jth position
int target= ans | ((1<<j)-1); //all 1 upto j-1 index
int cont=(1<<30)-1;
for(auto &num:nums){
cont &=num;
if((cont |target)==target) {cnt++; cont=(1<<30)-1;}
}
if(n-cnt >k){//if no of 1 at jth pos < k we can make it 0 otheriwse it becomes 1
ans|=(1<<j);
}
}
return ans;
}
};Editor is loading...
Leave a Comment