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 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++ 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;
}
// C++ program to Maximize count of 0s in left and 1s in
// right substring by splitting given Binary string
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 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 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# 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 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));
Output
5