Maximum difference of zeros and ones in binary string in C++


Given the task is to find a sub-string from a given binary string and then the maximum difference between the number of zeroes and the ones.

Let’s now understand what we have to do using an example −

Input

str = “100100110”

Output

2

Explanation

In the sub-array from the position 1 to 5 (“00100”), the difference between the zeros and ones = 4 – 1 = 3 which is the maximum that can be found.

Input

str = “00000”

Output

5

Approach used in the below program as follows

  • In main() function create a string str to store the binary string. Also initialize a variable int size to store the size of string and pass both of them into the Max() function.

  • In Max() function first check if all the elements are 1 by calling the One() function.

  • Create One() function of type bool and inside it create a variable int O = 0.

  • Loop from i = 0 till i< str.size() and if str[i] == 1 then add 1 to variable O.

  • Outside the loop, check if(O == size). If so, then return true.

  • Back in the Max() function if the One() function returned true then, return -1 as the answer.

  • Else proceed to find length. Initialize an array int a[100] = { 0 }.

  • Loop from i = 0 till i< size and put a[i] = (str[i] == '0' ? 1 : -1) and check every element of str this way.

  • Outside the loop, initialize another array int arr[100][3] and replace all its elements by -1 using memset(arr, -1, sizeof arr) and finally call Length(a, str, size, 0, 0, arr)

  • In Length() function, first check if (i >= size), if so, then it means the string is over and return 0.

  • Then check if (arr[i][s] != -1). If so, then it means the state has been already calculated and return arr[i][s].

  • Then check if(s == 0). If so, then return arr[i][s] = max(a[i] + Length(a, str, size, i + 1, 1, arr), Length(a, str, size, i + 1, 0, arr));

  • Else return arr[i][s] = max(a[i] + Length(a, str, size, i + 1, 1, arr), 0);

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
bool One(string str, int size){
   int O = 0;
   for (int i = 0; i < str.size(); i++)
      O += (str[i] == '1');
   return (O == size);
}
int Length(int a[], string str, int size,
int i, int s, int arr[][3]){
   // If string is over.
   if (i >= size)
      return 0;
      // If the already calculated.
   if (arr[i][s] != -1)
      return arr[i][s];
   if (s == 0)
      return arr[i][s] = max(a[i] +
      Length(a, str, size, i + 1, 1, arr),
      Length(a, str, size, i + 1, 0, arr));
   else
      return arr[i][s] = max(a[i] +
      Length(a, str, size, i + 1, 1, arr), 0);
}
int Max(string str, int size){
   // Checking if all elements are 1
   if (One(str, size))
      return -1;
      // Finding length
   int a[100] = { 0 };
   for (int i = 0; i < size; i++)
      a[i] = (str[i] == '0' ? 1 : -1);
      int arr[100][3];
      memset(arr, -1, sizeof arr);
   return Length(a, str, size, 0, 0, arr);
}
// main function
int main(){
   string str = "100100110";
   int size = 9;
   cout << Max(str, size);
   return 0;
}

Output

3

Updated on: 04-Aug-2020

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements