JAVA | Bit-Array | Fastest | EasyToUnderStand | O(N)
Please VoteUp if you find the solution helpful :)\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nJust need to store the latest or
Please VoteUp if you find the solution helpful :)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nJust need to store the latest or most recent position any of the 32 bits are set for each value. If none are set answer is 1 else the max latest position which can set a bit .\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N)\n# Code\n```\nclass Solution {\n    public int[] smallestSubarrays(int[] nums) {\n        int maxBit = 0, maxPos = nums.length;\n        int bCntArr[] = new int[32];\n        Arrays.fill(bCntArr, -1);\n        int res[] = new int[nums.length];\n        for(int i = nums.length - 1; i >= 0; i--) {\n            int curCnt = getMinPos(nums[i], bCntArr, i);\n            if(curCnt == -1)\n                res[i] = 1;\n            else\n                res[i] = curCnt - i + 1;\n        } \n        return res;\n    }\n\n    int getMinPos(int n, int arr[], int pos) {\n        int res = -1;\n        for(int i = 0; i < 32; i++) {\n            if((n & (1<<i)) > 0){\n                arr[i] = pos;\n            }\n            res = Math.max(res, arr[i]);\n        }\n        return res;\n    }\n}\n```
['Array', 'Bit Manipulation', 'Java']