Maximize count of 0s in left and 1s in right substring by splitting given Binary string
Last Updated :
24 Feb, 2025
Given a binary string str, the task is to split the given binary string at any index into two non-empty substrings such that the sum of counts of 0s in the left substring and 1s in the right substring is maximum. Print the sum of such 0s and 1s in the end.
Examples:
Input: str = "0011110011"
Output: 8
Explanation:
If a string is split at index 1, 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 the string is split at index 2, then the Left substring is "000" and the right substring is "1101111011".
Therefore, the sum of the count of 0s in left substring and 1s in right substring is 3 + 8 = 11.
[Naive Approach] - Consider Every Cut and Count - O(n2) Time and O(1) Space
The idea is to consider every cut and count 0s on left and 1st on right. At the end return the maximum count.
[Expected Approach] - Count All Ones - O(n) Time and O(1) Space
The idea is to find the optimal split point that maximizes the sum of zeros in the left substring and ones in the right substring.
We first count total ones in the string, then iterate through each possible split point (except the last character to ensure right substring isn't empty).
At each point, we maintain counts of zeros and ones seen so far in the left part. The maximum sum at any split point will be (zeros in left) + (total ones - ones in left), where (total ones - ones in left) gives us the ones in right substring. We track the maximum such sum found across all split points.
Below is the implementation of the above approach:
C++
// C++ program to Maximize count of 0s in left and 1s in
// right substring by splitting given Binary string
#include <bits/stdc++.h>
using namespace std;
// Function to maximize the sum of the count
// of zeros and ones in the left and right
// substring
int maxSum(string& str) {
int ans = 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 in the left substring.
int zero = 0, ones = 0;
// Iterate the given string and
// update the maximum sum
for (int i = 0; i<str.length()-1; i++) {
if (str[i] == '0') {
zero++;
}
else {
ones++;
}
// Update the maximum Sum
ans = max(ans, zero + (totalOnes - ones));
}
return ans;
}
int main() {
string str = "011101";
cout << maxSum(str);
return 0;
}
Java
// Java program to Maximize count of 0s in left and 1s in
// right substring by splitting given Binary string
class GfG {
// Function to maximize the sum of the count
// of zeros and ones in the left and right
// substring
static int maxSum(String str) {
int ans = 0;
// To store the total ones
int totalOnes;
// Count the total numbers of ones
// in string str
totalOnes = (int) str.chars().filter(ch -> ch == '1').count();
// To store the count of zeros and
// ones in the left substring.
int zero = 0, ones = 0;
// Iterate the given string and
// update the maximum sum
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == '0') {
zero++;
} else {
ones++;
}
// Update the maximum Sum
ans = Math.max(ans, zero + (totalOnes - ones));
}
return ans;
}
public static void main(String[] args) {
String str = "011101";
System.out.println(maxSum(str));
}
}
Python
# Python program to Maximize count of 0s in left and 1s in
# right substring by splitting given Binary string
# Function to maximize the sum of the count
# of zeros and ones in the left and right
# substring
def maxSum(str):
ans = 0
# To store the total ones
totalOnes = str.count('1')
# To store the count of zeros and
# ones in the left substring.
zero, ones = 0, 0
# Iterate the given string and
# update the maximum sum
for i in range(len(str) - 1):
if str[i] == '0':
zero += 1
else:
ones += 1
# Update the maximum Sum
ans = max(ans, zero + (totalOnes - ones))
return ans
if __name__ == "__main__":
str = "011101"
print(maxSum(str))
C#
// C# program to Maximize count of 0s in left and 1s in
// right substring by splitting given Binary string
using System;
using System.Linq;
class GfG {
// Function to maximize the sum of the count
// of zeros and ones in the left and right
// substring
static int maxSum(string str) {
int ans = 0;
// To store the total ones
int totalOnes;
// Count the total numbers of ones
// in string str
totalOnes = str.Count(ch => ch == '1');
// To store the count of zeros and
// ones in the left substring.
int zero = 0, ones = 0;
// Iterate the given string and
// update the maximum sum
for (int i = 0; i < str.Length - 1; i++) {
if (str[i] == '0') {
zero++;
} else {
ones++;
}
// Update the maximum Sum
ans = Math.Max(ans, zero + (totalOnes - ones));
}
return ans;
}
static void Main() {
string str = "011101";
Console.WriteLine(maxSum(str));
}
}
JavaScript
// JavaScript program to Maximize count of 0s in left and 1s in
// right substring by splitting given Binary string
// Function to maximize the sum of the count
// of zeros and ones in the left and right
// substring
function maxSum(str) {
let ans = 0;
// To store the total ones
let totalOnes = [...str].filter(ch => ch === '1').length;
// To store the count of zeros and
// ones in the left substring.
let zero = 0, ones = 0;
// Iterate the given string and
// update the maximum sum
for (let i = 0; i < str.length - 1; i++) {
if (str[i] === '0') {
zero++;
} else {
ones++;
}
// Update the maximum Sum
ans = Math.max(ans, zero + (totalOnes - ones));
}
return ans;
}
let str = "011101";
console.log(maxSum(str));
Similar Reads
Split a Binary String such that count of 0s and 1s in left and right substrings is maximum Given a binary string, str of length N, the task is to find the maximum sum of the count of 0s on the left substring and count of 1s on the right substring possible by splitting the binary string into two non-empty substrings. Examples: Input: str = "000111" Output: 6 Explanation: Splitting the bina
7 min read
Maximize sum by splitting given binary strings based on given conditions Given two binary strings str1 and str2 each of length N, the task is to split the strings in such a way that the sum is maximum with given conditions. Split both strings at the same position into equal length substrings.If both the substrings have only 0's then the value of that substring to be adde
7 min read
Maximize partitions in given Binary String having same ratio of 0s and 1s Given a binary string S of size N, the task is to find the maximum number of partitions of string S such that the ratio of the number of 0 and 1 in all the partitions are the same. Examples: Input: S = "010100001"Output: 3Explanation: The substring [0, 2], [3, 5] and [6, 8] have the same ratio of '0
9 min read
Maximum count of unique index 10 or 01 substrings in given Binary string Given a binary string str of length N, the task is to count the maximum number of adjacent pairs of form "01" or "10" that can be formed from the given binary string when one character can be considered for only one pair. Note: Adjacent pair means pair formed using adjacent characters. Examples: Inp
5 min read
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read