1151. Minimum Swaps to Group All 1's Together
Last updated
Was this helpful?
Last updated
Was this helpful?
Given a binary array data
, return the minimum number of swaps required to group all 1
’s present in the array together in any place in the array.
1 <= data.length <= 105
data[i]
is 0
or 1
.
GeeksforGeeks
ProgramCreek
YouTube
Input: data = [1, 0, 1, 0, 1]
Output: 1
Explanation:
There are 3 ways to group all 1's together:
[1, 1, 1, 0, 0] using 1 swap.
[0, 1, 1, 1, 0] using 2 swaps.
[0, 0, 1, 1, 1] using 1 swap.
The minimum is 1.