Created
July 17, 2024 19:16
-
-
Save SuryaPratapK/4861540245f00cf1f88b0ff8563883e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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