[C++] || Sliding Window || 100% faster
Same as Last Contests\nhttps://leetcode.com/problems/longest-nice-subarray/discuss/2527311/c-sliding-window-bit\n\nCODE\n\nclass Solution {\npublic:\n\tbool che
**Same as Last Contests**\nhttps://leetcode.com/problems/longest-nice-subarray/discuss/2527311/c-sliding-window-bit\n\n**CODE**\n```\nclass Solution {\npublic:\n\tbool check(vector<int> &maxiv , int len , vector<int> &vis) {\n\t\tfor (int i = 0 ; i <= len ; i++) {\n\t\t\tif (maxiv[i] != 0 and vis[i] == 0) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tvoid add(int a , int len , vector<int> &vis) {\n\t\tfor (int i = 0 ; i <= len ; i++) {\n\t\t\tif (((a >> i) & 1) == 1) vis[i]++;\n\t\t}\n\t}\n\tvoid addmaxi(int a , int len , vector<int> &maxiv) {\n\t\tfor (int i = 0 ; i <= len ; i++) {\n\t\t\tif (((a >> i) & 1) == 1) maxiv[i]++;\n\t\t}\n\t}\n\n\tvoid clear(int a , int len , vector<int> &vis) {\n\t\tfor (int i = 0 ; i <= len ; i++) {\n\t\t\tif (((a >> i) & 1) == 1) vis[i]--;\n\t\t}\n\t}\n\tvoid clearfrom(int a , int len , vector<int> &maxiv) {\n\t\tfor (int i = 0 ; i <= len ; i++) {\n\t\t\tif (((a >> i) & 1) == 1) maxiv[i]--;\n\t\t}\n\t}\n\n\tbool empty(vector<int> &maxiv , int len) {\n\t\tfor (int i = 0 ; i <= len ; i++) {\n\t\t\tif (maxiv[i] != 0) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tvector<int> smallestSubarrays(vector<int>& nums) {\n        \n        // calc maximum bits in nums\n        int maxi = 0;\n        for(auto x : nums) maxi = maxi|x;\n\n\t\tint n = nums.size() , i =0  , j= 0 , len;\n\t\tif (maxi != 0) len = log2(maxi);\n\t\telse len = 0;\n\n\n\n\t\tvector<int> maxiv(len + 1, 0);\n\t\tfor (auto x : nums) {\n\t\t\taddmaxi(x, len, maxiv);\n\t\t}\n\n\n\t\t// checking visited bits\n\t\tvector<int> vis(len + 1, 0);\n\n\n\t\t//returning ans\n\t\tvector<int> ans(n, 0);\n\n\t\twhile (i < n and j < n) {\n\n\t\t\t// maxiv is empty then all elements after is 0\n\t\t\tif (empty(maxiv, len)) j++;\n\n\n\t\t\t// checking for window\n\t\t\twhile (!check(maxiv, len, vis) and j < n) {\n\t\t\t\tadd(nums[j], len, vis);\n\t\t\t\tj++;\n\t\t\t}\n\n\n\n\t\t\tif (j < n and check(maxiv, len, vis)) {\n\t\t\t\tans[i] = j - i;\n\t\t\t\tclear(nums[i], len, vis);\n\t\t\t\tclearfrom(nums[i], len, maxiv);\n\t\t\t\ti++;\n\t\t\t}\n\n\t\t}\n\n\n\t\t// remaining last elements which ended at n\n\t\twhile (i < j) {\n\t\t\tans[i] = j - i;\n\t\t\tclear(nums[i], len, vis);\n\t\t\ti++;\n\t\t}\n\n\t\treturn ans;\n\t}\n};\n\n```
['C']