Maximize count of 0s in left and 1s in right substring by splitting given Binary string
Given binary string str, the task is to maximize the count of 0s in the left substring and 1s in the right substring by splitting the 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 a string is split 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 the string is split at index 3, 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.
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++
#include <bits/stdc++.h>
using namespace std;
int maxSum(string& str)
{
int maximumSum = 0;
int totalOnes;
totalOnes = count(str.begin(),
str.end(), '1' );
int zero = 0, ones = 0;
for ( int i = 0; str[i]; i++) {
if (str[i] == '0' ) {
zero++;
}
else {
ones++;
}
maximumSum
= max(
maximumSum,
zero + (totalOnes - ones));
}
return maximumSum;
}
int main()
{
string str = "011101" ;
cout << maxSum(str);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxSum(String str)
{
int maximumSum = 0 ;
int totalOnes = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == '1' )
{
totalOnes++;
}
}
int zero = 0 , ones = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == '0' )
{
zero++;
}
else
{
ones++;
}
maximumSum = Math.max(maximumSum,
zero + (totalOnes - ones));
}
return maximumSum;
}
public static void main(String args[])
{
String str = "011101" ;
System.out.println(maxSum(str));
}
}
|
Python3
def maxSum( str ):
maximumSum = 0
totalOnes = 0
for i in str :
if i = = '1' :
totalOnes + = 1
zero = 0
ones = 0
i = 0
while i < len ( str ):
if ( str [i] = = '0' ):
zero + = 1
else :
ones + = 1
maximumSum = max (maximumSum,zero + (totalOnes - ones))
i + = 1
return maximumSum
if __name__ = = '__main__' :
str = "011101"
print (maxSum( str ))
|
C#
using System;
class GFG{
static int maxSum( string str)
{
int maximumSum = 0;
int totalOnes = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '1' )
{
totalOnes++;
}
}
int zero = 0, ones = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '0' )
{
zero++;
}
else
{
ones++;
}
maximumSum = Math.Max(maximumSum, zero +
(totalOnes -
ones));
}
return maximumSum;
}
public static void Main( string []args)
{
string str = "011101" ;
Console.Write(maxSum(str));
}
}
|
Javascript
<script>
function maxSum(str)
{
var maximumSum = 0;
var totalOnes = 0;
str.split( '' ).forEach(c => {
if (c == '1' )
totalOnes++;
});
var zero = 0, ones = 0;
for ( var i = 0; str[i]; i++) {
if (str[i] == '0' ) {
zero++;
}
else {
ones++;
}
maximumSum
= Math.max(
maximumSum,
zero + (totalOnes - ones));
}
return maximumSum;
}
var str = "011101" ;
document.write( maxSum(str));
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Last Updated :
19 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...