Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5701 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 938 |

@bt8816103042 | 739 |

@SHOAIBVIJAPURE | 428 |

@codeantik | 412 |

@SherlockHolmes3 | 407 |

@neverevergiveup | 348 |

@mahlawatep | 347 |

@shalinibhataniya1097 | 343 |

@murarry3625 | 333 |

@saiujwal13083 | 326 |

@rohitanand | 314 |

Complete Leaderboard |

Given a binary string S which only contains '0' and '1' such that the '0' or '1' at the 1^{st }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 01011010**1 **
is changed to 0101101**1**0.In the second move
0101101**1**0 is changed to 01011**1**100. In the
third move 0**1**0111100 is change to 00**1**111100.

**Example 2:**

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

**Your Task:**

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

Login to report an issue on this page.

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

Yes
Cluster all the 1 in a given string

...