
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Maximum difference of zeros and ones in binary string - (O(n) time) in C++
- Count of substrings of a binary string containing K ones in C++
- Maximum Number of Ones in C++
- Maximum consecutive one’s (or zeros) in a binary circular array in C++
- Maximum Consecutive Zeroes in Concatenated Binary String in C++
- Move all zeros to start and ones to end in an Array of random integers in C++
- Create an array with ones above the main diagonal and zeros elsewhere in Numpy
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Program to find maximum binary string after change in python
- Return a 2-D array with ones on the diagonal and zeros elsewhere in Numpy
- C++ Queries to Answer the Number of Ones and Zeros to the Left of Given Index
- Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the lower diagonal and zeros elsewhere in Numpy
- Maximum length of consecutive 1’s in a binary string in Python using Map function
