C++ | Binary search | with comments
\n\nclass Solution {\npublic:\n    vector<int> smallestSubarrays(vector<int>& nums) {\n        int sz=nums.size();\n        vector<vector<int>>arr(31);\n       
```\n\nclass Solution {\npublic:\n    vector<int> smallestSubarrays(vector<int>& nums) {\n        int sz=nums.size();\n        vector<vector<int>>arr(31);\n        int j=0;\n        // arr[i] will store the indices of values in which ith bit is on\n        for(auto & ele:nums){\n            for(int i=0;i<31;i++){\n                if((ele>>i)&1)\n                    arr[i].push_back(j);\n            }\n            j++;\n        }\n        // suf[i] will store maximum or starting at this index\n        vector<int>suf=nums;\n        for(int i=sz-2;i>=0;i--)\n            suf[i]=nums[i]|suf[i+1];\n        vector<int>ans(sz,0);\n        for(int i=0;i<sz;i++){\n            // assume max OR value starting at this index is "x", then we will check if "kth" bit is on or off,\n            // if its on then we want to find the minimum greater index which can set its bit on,we are doing this using binary search.\n            int res=1;\n            for(int k=0;k<31;k++){\n                if((suf[i]>>k)&1){\n                    auto it=lower_bound(begin(arr[k]),end(arr[k]),i);\n                    int len=(*it)-i+1;\n                    res=max(res,len);\n                }\n            }\n            ans[i]=res;\n        }\n        return ans;\n    }\n};\n```\nDo **UPVOTE** is it helps:)\n
['C']