# Removing elements between the two zeros

Given an integer N which shows the size of the string and in the next line given a string which contain string of character with only zero and one. Task is to remove single character each time which comes in between the two zero character.

During each turn, only one character from the string will be removed that satisfies the following condition :

• It must be surrounded by zeroes on both sides.

Examples:

```Input  : str = "1001
Output : str = "1001"

Input  : str = "10101
Output : str = "1001"
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Use a loop from 1 to N – 1 and check if any element lies between two zeros such that s[i – 1] = ‘0’ and s[i + 1] = ‘0’. If the condition is satisfied then, delete the character at that position, ad start searching for pattern again.

## C++

 `// CPP code to delete elements between zeros ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the string  ` `// after operation ` `string findstring(string s) ` `{ ` `    ``int` `n = s.length(); ` `         `  `    ``// Travesing through string  ` `    ``for` `(``int` `i = 1; i < n - 1; i++) ` `    ``{ ` `        ``// Checking for character  ` `        ``// Between two zeros ` `        ``if` `((s.at(i - 1) == ``'0'` `&&  ` `            ``s.at(i + 1) == ``'0'``)) ` `        ``{ ` ` `  `            ``// deleting the charater  ` `            ``// At specific position ` `            ``s.erase(i, 1); ` `             `  `            ``i--; ` `            ``if` `(i > 0 && s.at(i - 1) == ``'0'``) ` `                 ``i--; ` `                         `  `            ``// updating the length  ` `            ``// of the string ` `            ``n = s.length(); ` `        ``} ` `    ``} ` `             `  `    ``return` `s; ` `} ` `     `  `// Drivers code ` `int` `main() { ` `     `  `    ``cout << findstring(``"100100"``); ` `    ``return` `0; ` `} `

## Java

 `// Java code to delete elements between zeros ` `import` `java.util.*; ` ` `  `public` `class` `GFG  ` `{ ` `    ``// Function to find the string  ` `    ``// after operation ` `    ``static` `String findstring(String s) ` `    ``{ ` `        ``int` `n = s.length(); ` `             `  `        ``// use for loop to remove the ` `        ``// character between two zeros ` `        ``for` `(``int` `i = ``1``; i < n - ``1``; i++) ` `        ``{  ` `            ``// Checking for character  ` `            ``// Between two zeros ` `            ``if` `((s.charAt(i - ``1``) == ``'0'` `&&  ` `                 ``s.charAt(i + ``1``) == ``'0'``)) ` `            ``{ ` `                     `  `                ``// deleting the charater  ` `                ``// At specific position ` `                ``s = s.substring(``0``, i) + s.substring(i + ``1``); ` `                 `  `                ``i--; ` `                ``if` `(i > ``0` `&& s.charAt(i - ``1``) == ``'0'``) ` `                    ``i--; ` `                         `  `                ``// updating the length  ` `                ``// of the string ` `                ``n = s.length(); ` `            ``} ` `        ``} ` `             `  `        ``return` `s; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `       ``String s=``"10000"``; ` `       ``System.out.println(findstring(s)); ` `    ``} ` `} `

## C#

 `// C# code to delete  ` `// elements between zeros ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// Function to find the  ` `    ``// string after operation ` `    ``static` `string` `findstring(``string` `s) ` `    ``{ ` `        ``int` `n = s.Length; ` `        ``string` `st = ``""``; ` `         `  `        ``// Travesing through string  ` `        ``for` `(``int` `i = 1; i < n - 1; i++) ` `        ``{ ` `            ``// Checking for character  ` `            ``// Between two zeros ` `            ``if` `((s[i - 1] == ``'0'` `&&  ` `                 ``s[i + 1] == ``'0'``)) ` `            ``{ ` `     `  `                ``// deleting the charater  ` `                ``// At specific position ` `                ``st = s.Remove(i, 1); ` `                ``s = st; ` `                 `  `                ``i--; ` `                ``if` `(i > 0 &&  ` `                    ``s[i - 1] == ``'0'``) ` `                    ``i--; ` `                             `  `                ``// updating the length  ` `                ``// of the string ` `                ``n = s.Length; ` `            ``} ` `        ``}                  ` `        ``return` `s; ` `    ``} ` `     `  `    ``// Driver code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``Console.Write(findstring(``"100100"``)); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

Output:

```100
```

Time Complexity : O(N), where N is the size of input string.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : manishshaw1, shubham_singh

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.