Showing:
Handle Score
@Ibrahim Nash 5761
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4944
@saiujwal13083 4506
@sanjay05 3762
@marius_valentin_dragoi 3516
@sushant_a 3459
@verma_ji 3341
@KshamaGupta 3318
Cluster all the 1 in a given string
Easy Accuracy: 0.0% Submissions: 0 Points: 2

Given a binary string S which only contains '0' and '1' such that the '0' or '1' at the 1st position is just next to '0' or '1' at the last position i.e. string is cyclic. Your task is to re arrange the string such that all the '1' in the string are next to each other (Note : the string is cyclic ).
You can choose any '1' in the string and swap its position with the first '1' in any of the two direction.
Find the minimum number of moves in which the task can be done.

Example 1:

Input: S = "010110101"
Output: 3
Explanation: In the first move 010110101
is changed to 010110110.In the second move
010110110 is changed to 010111100. In the
third move 010111100 is change to 001111100.


Example 2:

Input: S = "111"
Output: 0
Explanation: All one's are clustred. So answer will
be zero.


You don't need to read or print anything. Your task is to complete the function minimumMoves() which takes string S as input parameter and returns the minimum number of moves.

Expected Time Complexity:  O(|S|)
Expected Space Complexity: O(1)

Constraints:
1 ≤ |S| ≤ 100000

to report an issue on this page.

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?