Given a **binary number** in the form of a string, the task is to print a binary equivalent obtained by flipping only one contiguous set of 0s such that the decimal equivalent of this binary number is maximum.**Note:** Do not assume any trailing zeroes in the start of the binary number i.e. “0101” is given as “101”.**Examples:**

Input:s = “10101”Output:11101Explanation:

Here we can only flip the 2nd character of the string “10101” ( = 21) that will change it to “11101” (= 29). Since we are allowed to flip a continuous subarray, any more flipping will lead to decrease in decimal equivalent.Input:s = “1000”Output:1111Explanation:

If we flip the continuous characters starting from position 1 till 3 we will get 1111 which is the maximum number possible in 4 bits i.e. 15.

**Approach:** To solve the problem mentioned above we know that we have to increase the value of the binary equivalent. Therefore, we must **increase the number of 1’s at higher position**. Clearly, we can increase the value of the number by only flipping the initially occurring zeroes. Traverse the string and flip the first occurrence of zeroes until a 1 occurs, in this case, the loop must break. Print the resultant string.**Below is the implementation of the above approach:**

## C++

`// C++ implementation to Maximize the value of` `// the decimal equivalent given in the binary form` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to print the binary number` `void` `flip(string& s)` `{` ` ` `for` `(` `int` `i = 0; i < s.length(); i++) {` ` ` `// Check if the current number is 0` ` ` `if` `(s[i] == ` `'0'` `) {` ` ` `// Find the continuous 0s` ` ` `while` `(s[i] == ` `'0'` `) {` ` ` `// Replace initially` ` ` `// occurring 0 with 1` ` ` `s[i] = ` `'1'` `;` ` ` `i++;` ` ` `}` ` ` `// Break out of loop if 1 occurs` ` ` `break` `;` ` ` `}` ` ` `}` `}` `// Driver code` `int` `main()` `{` ` ` `string s = ` `"100010001"` `;` ` ` `flip(s);` ` ` `cout << s;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to maximize the value of` `// the decimal equivalent given in the binary form` `import` `java.util.*;` `class` `GFG{` `// Function to print the binary number` `static` `void` `flip(String s)` `{` ` ` `StringBuilder sb = ` `new` `StringBuilder(s);` ` ` `for` `(` `int` `i = ` `0` `; i < sb.length(); i++) ` ` ` `{` ` ` ` ` `// Check if the current number is 0` ` ` `if` `(sb.charAt(i) == ` `'0'` `)` ` ` `{` ` ` ` ` `// Find the continuous 0s` ` ` `while` `(sb.charAt(i) == ` `'0'` `)` ` ` `{` ` ` ` ` `// Replace initially` ` ` `// occurring 0 with 1` ` ` `sb.setCharAt(i, ` `'1'` `);` ` ` `i++;` ` ` `}` ` ` ` ` `// Break out of loop if 1 occurs` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `System.out.println(sb.toString());` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `String s = ` `"100010001"` `;` ` ` `flip(s);` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to ` `# Maximize the value of the ` `# decimal equivalent given ` `# in the binary form ` `# Function to print the binary ` `# number` `def` `flip(s):` ` ` `s ` `=` `list` `(s)` ` ` `for` `i ` `in` `range` `(` `len` `(s)):` ` ` `# Check if the current number ` ` ` `# is 0` ` ` `if` `(s[i] ` `=` `=` `'0'` `):` ` ` `# Find the continuous 0s` ` ` `while` `(s[i] ` `=` `=` `'0'` `):` ` ` `# Replace initially ` ` ` `# occurring 0 with 1 ` ` ` `s[i] ` `=` `'1'` ` ` `i ` `+` `=` `1` ` ` `s ` `=` `''.join(` `map` `(` `str` `, s))` ` ` `# return the string and ` ` ` `# break the loop` ` ` `return` `s` `# Driver code ` `s ` `=` `"100010001"` `print` `(flip(s))` `# This code is contributed by avanitrachhadiya2155` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to maximize the value of` `// the decimal equivalent given in the binary form` `using` `System;` `class` `GFG{` `// Function to print the binary number` `static` `String flip(` `char` `[]s)` `{` ` ` `for` `(` `int` `i = 0; i < s.Length; i++)` ` ` `{` ` ` ` ` `// Check if the current number is 0` ` ` `if` `(s[i] == ` `'0'` `)` ` ` `{` ` ` ` ` `// Find the continuous 0s` ` ` `while` `(s[i] == ` `'0'` `)` ` ` `{` ` ` ` ` `// Replace initially` ` ` `// occurring 0 with 1` ` ` `s[i] = ` `'1'` `;` ` ` `i++;` ` ` `}` ` ` ` ` `// Break out of loop if 1 occurs` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `return` `new` `String(s);` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` `String s = ` `"100010001"` `;` ` ` ` ` `Console.WriteLine(flip(s.ToCharArray()));` `}` `}` `// This code is contributed by Rohit_ranjan` |

*chevron_right*

*filter_none*

**Output:**

111110001

## Recommended Posts:

- Decimal Equivalent of Gray Code and its Inverse
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Maximize sum of an Array by flipping sign of all elements of a single subarray
- Maximize sum of assigned weights by flipping at most K bits in given Binary String
- Maximize count of rows consisting of equal elements by flipping columns of a Matrix
- Largest sum contiguous subarray having only non-negative elements
- Convert Decimal To Hexa-Decimal including negative numbers
- Number formed by flipping all bits to the left of rightmost set bit
- Numbers formed by flipping common set bits in two given integers
- Convert the ASCII value sentence to its equivalent string
- Check whether two strings are equivalent or not according to given condition
- Check whether the binary equivalent of a number ends with "001" or not
- Convert the given BCD to its equivalent Binary form
- Check whether the binary equivalent of a number ends with given string or not
- Convert given Float value to equivalent Fraction
- Convert a sentence into its equivalent mobile numeric keypad sequence

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.