Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
How many 1's should be grouped together ? Is not a fixed number?
Yeah it's just the number of 1's the whole array has. Let's name this number as ones
Every subarray of size of ones, needs some number of swaps to reach, Can you find the number of swaps needed to group all 1's in this subarray?
It's the number of zeros in that subarray.
Do you need to count the number of zeros all over again for every position ?
Use Sliding Window technique.