Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Notice that the number of 1’s to be grouped together is fixed. It is the number of 1's the whole array has.
Call this number total. We should then check for every subarray of size total (possibly wrapped around), how many swaps are required to have the subarray be all 1’s.
The number of swaps required is the number of 0’s in the subarray.
To eliminate the circular property of the array, we can append the original array to itself. Then, we check each subarray of length total.
How do we avoid recounting the number of 0’s in the subarray each time? The Sliding Window technique can help.