Ruby O(n) counting last seen index for each bit
ApproachGo through the nums array in reverse order. Save the last seen position of each bit. For each position, calculate the distance between the current posit
# Approach
<!-- Describe your approach to solving the problem. -->
Go through the nums array in reverse order. Save the last seen position of each bit. For each position, calculate the distance between the current position and the index of each set bit. Use the maximum distance, when all bits are set, as the result for the current index.


# Code
```ruby []
# @param {Integer[]} nums
# @return {Integer[]}
def smallest_subarrays(nums)
  n = nums.length

  # Calculate the smallest subarray length for each position
  result = Array.new(n, 1)
  last_seen = Array.new(32, -1)

  (n - 1).downto(0) do |i|
    nums[i].bit_length.times do |bit|
      last_seen[bit] = i if nums[i][bit] == 1
    end

    max_index = i
    last_seen.each do |index|
      max_index = [max_index, index].max if index != -1
    end

    result[i] = max_index - i + 1
  end

  result
end
```
['Ruby']