Maximize count of 0s in left and 1s in right substring by splitting given Binary string
Given a binary string str, the task is to maximize the count of 0s in left substring and 1s in right substring by splitting given Binary string at any index. Print the sum of such 0s and 1s in the end.
Examples:
Input: str = “0011110011”
Output: 8
Explanation:
If string is splitted at index 2, then Left substring = “00” and Right substring = “11110011”.
Therefore the sum of the count of 0s in left substring and 1s in right substring is 2 + 6 = 8.
Input: str = “0001101111011”
Output: 11
Explanation:
If string is splitted at index 3, then Left substring is “000” and Right substring is “1101111011”.
Therefore the sum of the count of 0s in left substring and 1s in right substring is 3 + 8 = 11.
Approach:
- Count the number of ones in the given binary string str (say totalOnes).
- Traverse the given string and keep the counts of 0s (say zero) and 1s (say one).
- During the string traversal update the maximum sum as:
maxSum = max(maxSum, zero + (totalOnes – one))
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to maximise the sum of the count // of zeros and ones in the left and right // substring int maxSum(string& str) { int maximumSum = 0; // To store the total ones int totalOnes; // Count the total numbers of ones // in string str totalOnes = count(str.begin(), str.end(), '1'); // To store the count of zeros and // ones while traversing string int zero = 0, ones = 0; // Interate the given string and // update the maximum sum for (int i = 0; str[i]; i++) { if (str[i] == '0') { zero++; } else { ones++; } // Update the maximum Sum maximumSum = max( maximumSum, zero + (totalOnes - ones)); } return maximumSum; } // Driver Code int main() { // Given binary string string str = "011101"; // Function call cout << maxSum(str); return 0; } |
5
Time Complexity: O(N), where N is the length of the string.
Recommended Posts:
- Minimum steps to remove substring 010 from a binary string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Decode a string recursively encoded as count followed by substring
- Splitting a Numeric String
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count of substrings of a binary string containing K ones
- Count of 1-bit and 2-bit characters in the given binary string
- Count of cyclic permutations having XOR with other binary string as 0
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Given a binary string, count number of substrings that start and end with 1.
- Count of operations to make a binary string"ab" free
- Rearrange given string to maximize the occurrence of string t
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.

