Given a binary string **S** of length **N**, the task is to print the minimum number of indices, substrings consisting only of **1s** are required to be shifted such that all **1**s present in the string are grouped together.

**Examples:**

Input:S = “00110111011”Output:2Explanation:Operation 1:Shift substring {S[2], S[3]} (0-based indexing) to right and place between S[4] and S[5]. Now, S is modified to “00011111011”.Operation 2:Shift substring {S[10], S[11]} to left and place between S[7] and S[8]. Now, S is modified to “00011111110”.

Therefore, 2 substrings are required to be shifted.

Input:S = “1001001”Output:4Explanation:Operation 1:Shift ‘1’ at S[0] to right by two indices and place between S[2] and S[3]. Now, S is modified to “0011001”.Operation 2:Shift ‘1’ at S[6] to left by two indices and place between S[3] and S[4]. Now, S is modified to “0011100”.

Therefore, 4 substrings are required to be shifted.

**Approach:** The problem can be solved by observing that the minimum number of operations required is equal to the number of **0**s present between any pair of consecutive **1**s. Follow the steps below to solve the problem:

- Iterate over the characters of the string and search for the first and last occurrences of the character
**‘1’**and store it in variable, say**firstOne**and**lastOne**respectively. - Traverse the range
**[firstOne, lastOne]**and count the number of**‘0’**s present in the substring**{S[firstOne], .. , S[lastOne]}**and print it as the required output.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count indices substrings` `// of 1s need to be shifted such that` `// all 1s in the string are grouped together` `void` `countShifts(string str)` `{` ` ` `// Stores first occurrence of '1'` ` ` `int` `firstOne = -1;` ` ` `// Stores last occurrence of '1'` ` ` `int` `lastOne = -1;` ` ` `// Count of 0s between firstOne and lastOne` ` ` `int` `count = 0;` ` ` `// Traverse the string to find the` ` ` `// first and last occurrences of '1'` ` ` `for` `(` `int` `i = 0; i < str.length(); i++) {` ` ` `if` `(str[i] == ` `'1'` `) {` ` ` `if` `(firstOne == -1)` ` ` `firstOne = i;` ` ` `lastOne = i;` ` ` `}` ` ` `}` ` ` `if` `((firstOne == -1) || (firstOne == lastOne)) {` ` ` `cout << 0;` ` ` `return` `;` ` ` `}` ` ` `// Count number of 0s present between` ` ` `// firstOne and lastOne` ` ` `for` `(` `int` `i = firstOne; i <= lastOne; i++) {` ` ` `if` `(str[i] == ` `'0'` `) {` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print minimum operations` ` ` `cout << count << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given string` ` ` `string str = ` `"00110111011"` `;` ` ` `countShifts(str);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to count indices substrings ` `// of 1s need to be shifted such that ` `// all 1s in the string are grouped together ` `static` `void` `countShifts(String str) ` `{ ` ` ` ` ` `// Stores first occurrence of '1' ` ` ` `int` `firstOne = -` `1` `; ` ` ` ` ` `// Stores last occurrence of '1' ` ` ` `int` `lastOne = -` `1` `; ` ` ` ` ` `// Count of 0s between firstOne and lastOne ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Traverse the string to find the ` ` ` `// first and last occurrences of '1' ` ` ` `for` `(` `int` `i = ` `0` `; i < str.length(); i++) ` ` ` `{ ` ` ` `if` `(str.charAt(i) == ` `'1'` `)` ` ` `{` ` ` `if` `(firstOne == -` `1` `) ` ` ` `firstOne = i; ` ` ` ` ` `lastOne = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `((firstOne == -` `1` `) || (firstOne == lastOne))` ` ` `{ ` ` ` `System.out.print(` `0` `);` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Count number of 0s present between ` ` ` `// firstOne and lastOne ` ` ` `for` `(` `int` `i = firstOne; i <= lastOne; i++) ` ` ` `{ ` ` ` `if` `(str.charAt(i) == ` `'0'` `) ` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print minimum operations ` ` ` `System.out.println(count);` `} ` `// Driver code` `public` `static` `void` `main (String[] args)` `{` ` ` ` ` `// Given string ` ` ` `String str = ` `"00110111011"` `; ` ` ` ` ` `countShifts(str); ` `}` `}` `// This code is contributed by code_hunt` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` ` ` `# Function to count indices substrings` `# of 1s need to be shifted such that` `# all 1s in the string are grouped ` `# together` `def` `countShifts(` `str` `):` ` ` ` ` `# Stores first occurrence of '1'` ` ` `firstOne ` `=` `-` `1` ` ` ` ` `# Stores last occurrence of '1'` ` ` `lastOne ` `=` `-` `1` ` ` ` ` `# Count of 0s between firstOne` ` ` `# and lastOne` ` ` `count ` `=` `0` ` ` ` ` `# Traverse the string to find the` ` ` `# first and last occurrences of '1'` ` ` `for` `i ` `in` `range` `(` `len` `(` `str` `)): ` ` ` `if` `(` `str` `[i] ` `=` `=` `'1'` `): ` ` ` ` ` `if` `(firstOne ` `=` `=` `-` `1` `):` ` ` `firstOne ` `=` `i` ` ` ` ` `lastOne ` `=` `i` ` ` ` ` `if` `((firstOne ` `=` `=` `-` `1` `) ` `or` ` ` `(firstOne ` `=` `=` `lastOne)): ` ` ` `print` `(` `0` `)` ` ` `return` ` ` ` ` `# Count number of 0s present between` ` ` `# firstOne and lastOne` ` ` `for` `i ` `in` `range` `(firstOne, lastOne ` `+` `1` `, ` `1` `):` ` ` `if` `(` `str` `[i] ` `=` `=` `'0'` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Print minimum operations` ` ` `print` `(count)` `# Driver Code` `# Given string` `str` `=` `"00110111011"` `countShifts(` `str` `)` `# This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG ` `{` ` ` ` ` `// Function to count indices substrings ` ` ` `// of 1s need to be shifted such that ` ` ` `// all 1s in the string are grouped together ` ` ` `static` `void` `countShifts(` `string` `str) ` ` ` `{ ` ` ` `// Stores first occurrence of '1' ` ` ` `int` `firstOne = -1; ` ` ` ` ` `// Stores last occurrence of '1' ` ` ` `int` `lastOne = -1; ` ` ` ` ` `// Count of 0s between firstOne and lastOne ` ` ` `int` `count = 0; ` ` ` ` ` `// Traverse the string to find the ` ` ` `// first and last occurrences of '1' ` ` ` `for` `(` `int` `i = 0; i < str.Length; i++) ` ` ` `{ ` ` ` `if` `(str[i] == ` `'1'` `)` ` ` `{ ` ` ` ` ` `if` `(firstOne == -1) ` ` ` `firstOne = i; ` ` ` ` ` `lastOne = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `((firstOne == -1) || (firstOne == lastOne))` ` ` `{ ` ` ` `Console.Write(0);` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Count number of 0s present between ` ` ` `// firstOne and lastOne ` ` ` `for` `(` `int` `i = firstOne; i <= lastOne; i++) ` ` ` `{ ` ` ` ` ` `if` `(str[i] == ` `'0'` `) ` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print minimum operations ` ` ` `Console.WriteLine(count);` ` ` `} ` ` ` `// Driver code` ` ` `static` `void` `Main() ` ` ` `{` ` ` ` ` `// Given string ` ` ` `string` `str = ` `"00110111011"` `; ` ` ` ` ` `countShifts(str); ` ` ` `}` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.