a few solutions
Process the input array A from right-to-left.\n\n For each ith index, do we have what we need?\n    * If not, we fulfill the need with the left-most jth index b
Process the input array `A` from right-to-left.\n\n* For each `i`<sup>th</sup> index, do we `have` what we `need`?\n    * If not, we fulfill the `need` with the left-most `j`<sup>th</sup> index based upon the `last` seen `k`<sup>th</sup> bit needed.\n\nNote: `need` is the running total of bitwise-OR of values of the input array `A`.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun smallestSubarrays(A: IntArray): IntArray {\n        var N = A.size\n        var ans = IntArray(N)\n        var last = IntArray(32)  // last index of each 0..31 bit set (what we need to be added onto what we have)\n        var (have, need) = Pair(0, 0)\n        for (i in N - 1 downTo 0) {\n            var j = i\n            have = A[i]\n            need = A[i] or need\n            for (k in 0..31) {\n                if (have and (1 shl k) != 0) last[k] = i\n                if (need and (1 shl k) != 0) j = Math.max(j, last[k])\n            }\n            ans[i] = j - i + 1  // +1 for i..j inclusive\n        }\n        return ans\n    }\n}\n```\n\n*Javascript*\n```\nlet smallestSubarrays = (A, have = 0, need = 0) => {\n    let N = A.length;\n    let ans = Array(N);\n    let last = Array(32);  // last index of each 0..31 bit set (what we need to be added onto what we have)\n    for (let i = N - 1; 0 <= i; --i) {\n        let j = i;\n        have = A[i];\n        need = A[i] | need;\n        for (let k = 0; k < 32; ++k) {\n            if (have & (1 << k)) last[k] = i;\n            if (need & (1 << k)) j = Math.max(j, last[k]);\n        }\n        ans[i] = j - i + 1;  // +1 for i..j inclusive\n    }\n    return ans;\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def smallestSubarrays(self, A: List[int], need = 0) -> List[int]:\n        N = len(A)\n        ans = [0] * N\n        last = [0] * 32  # last index of each 0..31 bit set (what we need to be added onto what we have)\n        for i in range(N - 1, -1, -1):\n            j = i\n            have = A[i]\n            need = A[i] | need\n            for k in range(32):\n                if have & (1 << k): last[k] = i\n                if need & (1 << k): j = max(j, last[k])\n            ans[i] = j - i + 1  # +1 for i..j inclusive\n        return ans\n```\n\n*Rust*\n```\ntype VI = Vec<i32>;\nuse std::cmp::max;\nimpl Solution {\n    pub fn smallest_subarrays(A: VI) -> VI {\n        let N = A.len();\n        let mut ans = vec![0; N];\n        let mut last = vec![0; 32];  // last index of each 0..31 bit set (what we need to be added onto what we have)\n        let (mut have, mut need) = (0, 0);\n        for i in (0..N).rev() {\n            let mut j = i;\n            have = A[i];\n            need = A[i] | need;\n            for k in 0..32 {\n                if have & (1 << k) != 0 { last[k] = i; }\n                if need & (1 << k) != 0 { j = max(j, last[k]) }\n            }\n            ans[i] = (j - i + 1) as i32;  // +1 for i..j inclusive\n        }\n        ans\n    }\n}\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    VI smallestSubarrays(VI& A, int have = 0, int need = 0) {\n        int N = A.size();\n        VI ans(N);\n        VI last(32);  // last index of each 0..31 bit set (what we need to be added onto what we have)\n        for (auto i{ N - 1 }; 0 <= i; --i) {\n            auto j = i;\n            have = A[i];\n            need = A[i] | need;\n            for (auto k{ 0 }; k < 32; ++k) {\n                if (have & (1 << k)) last[k] = i;\n                if (need & (1 << k)) j = max(j, last[k]);\n            }\n            ans[i] = j - i + 1;  // +1 for i..j inclusive\n        }\n        return ans;\n    }\n};\n```
[]