Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/4861540245f00cf1f88b0ff8563883e4 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/4861540245f00cf1f88b0ff8563883e4 to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<int> smallestSubarrays(vector<int>& nums) {
vector<int> ans;
int n=nums.size();
vector<int> nextSetBitPos(32,-1);
//Solve from right to left
int maxOR=0;
for(int i=n-1;i>=0;--i){
maxOR|=nums[i];
int curr=nums[i];
int pos=0;
while(curr){
if(curr&1)
nextSetBitPos[pos]=i;
curr/=2;
pos++;
}
int max_idx=*max_element(nextSetBitPos.begin(),nextSetBitPos.end());
if(max_idx==-1) ans.push_back(1);//Base-case
else ans.push_back(max_idx-i+1);
}
reverse(ans.begin(),ans.end());
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment