simple clean C++ code || O(N) solution || Bit-Manipulation
Idea -By looking at how OR works, we can see that bits can only be turned on with 1. So, start from the end and keep track of the minimum index that will keep t
***Idea*** -*By looking at how OR works, we can see that bits can only be turned on with 1. So, start from the end and keep track of the minimum index that will keep the bit set. Then, take the maximum of all the indexes that contain set bits.*\nfor this purpose, we maintain an Array named nearest of 32 bits which indicate the index of the `nearest` array element which set the particular `jth` bit at that particular instance. then we find the farthest set bit we found so far.\nlet see an example -\n`nums[] = {9,13,55,48,56};`\nbit representation are as follow - \n```\ni   j--> 5 4 3 2 1 0\n--------------------\n9  ->    0 0 1 0 0 1\n13 ->    0 0 1 1 0 1\n55 ->    1 1 0 1 1 1\n48 ->    1 1 0 0 0 0\n56 ->    1 1 1 0 0 0\n\nlet see how we get length for first element (for 9) let\'s find it manually.\nat bit position 0 -> what is the nearest element who is set to 1. the answer is 9 itself so nearest[0] = 0 (i position of 9)\nat bit position 1 ->  what is the nearest element who is set to 1. the answer is 55(2nd element) nearest[1] = 2 (i position of 55)\nat bit position 2 ->  what is the nearest element who is set to 1. the answer is 13(1st element) nearest[2] = 1 (i position of 13)\nat bit position 3 ->  what is the nearest element who is set to 1. the answer is 9 itself so nearest[3] = 0 (i position of 9)\nat bit position 4 ->  what is the nearest element who is set to 1. the answer is 55(2nd element) nearest[4] = 2 (i position of 55)\nat bit position 5 ->  what is the nearest element who is set to 1. the answer is 55(2st element) nearest[5] = 2 (i position of 55)\n\nnearest[] = {0,2,1,0,2,2};\namong them the fathest nearest[j] = 2;\nso if we start OR operation from 0th index then first time we get our maximum value at 2nd index.\nso the smallest subset is nums[0..2] => of size 3;\n\nfor maintaining the nearest array we have to triverse nums from right to left\nand keep updating nearest[j] if current element have a set bit at jth position.\n```\n```\nclass Solution {\npublic:\n    vector<int> smallestSubarrays(vector<int>& nums) {\n        \n        int n = nums.size();\n        // for keeping track of last index of every bit till the ith index.\n        // total bit in a data type int is 32.\n        vector<int>nearest(32,-1);\n        vector<int>ans(n);\n        \n        for(int i = n-1; i>=0; i--){\n            for(int j = 0; j<32; j++){\n                // 1<<j -> a number with only set bit at jth position.\n                // nums[i]&(1<<j) checks whether jth bit is set or not of nums[i];\n                \n                // if jth bit of nums[i] is set then we update nearest[j] to i;\n                if(nums[i]&(1<<j)){\n                    nearest[j] = i;\n                }\n            }\n            \n            // initially set lastSetBit to i because we have to start our set with ith element.\n            int lastSetBit = i;\n\t\t\t\n            // now we have to find which one is the bit seted most farthest among all 32 bits. we need the index i for this bit.\n            for(int j = 0; j<32; j++){\n                // we keep updating lastSetBit if we get any greater "i" of set bit.\n                lastSetBit = max(nearest[j],lastSetBit);\n            }\n            // from last set bit only we can get smallest subarray.\n            // after this we get same value but our subarray size will increase.\n            ans[i] = lastSetBit-i+1;\n        }\n        \n        \n        return ans;\n    }\n};\n```\nPLEASE UPVOTE IF YOU FOUND IT HELPFUL\uD83D\uDE22\uD83D\uDE36\u200D\uD83C\uDF2B\uFE0F\uD83D\uDE36\u200D\uD83C\uDF2B\uFE0F
['Bit Manipulation', 'C', 'C++']