Maximize sum by splitting given binary strings based on given conditions
Last Updated :
12 Dec, 2022
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 added is 1.
- If both the substrings have only 1's then the value of that substring to be added is 0.
- If both the substrings have 0's and 1's then the value of that substring to be added is 2.
Return the maximum sum possible.
Examples:
Input: str1 = "0101000", str2 = "1101100"
Output: 8
Explanation:
Split like this:
Take "0" from str1 and "1" from str2 -> 2
Take "10" from str1 and "10" from str2 -> 2
Take "1" from str1 and "1" from str2 -> 0
Take "0" from str1 and "1" from str2 -> 2
Take "0" from str1 and "0" from str2 -> 1
Take "0" from str1 and "0" from str2 -> 1
Sum is 2 + 2 + 0 + 2 + 1 + 1 => 8
Input: str1 = "01", str2 = "01"
Output: 2
Approach: This problem can be solved using the greedy approach. First, take some of the distinct pair means (0, 1) because it gives the maximum value 2 or with a distinct value pair with the same value pair, the maximum value is 2 so there is no benefit. then try to make pair of (0, 0) with (1, 1) or vice versa to maximize the sum.
- Initialize the MaxSum to 0.
- Traverse the strings. If Ai is not equal to Bi, increment the MaxSum by 2.
- Traverse the strings again and try to pair with opposite value means 0 with 1 or 1 with 0.
- Check previous and next value of string if it's opposite increment MaxSum by 2.
- Else if the pair is only (0, 0) increment MaxSum by 1.
Below is the implementation of the above-mentioned approach:
C++
// C++ program to split two
// binary strings in such a way
// such that sum is maximum.
#include <iostream>
using namespace std;
// Function to split strings
// to find maximum sum
int MaxSum(string a, string b)
{
int ans = 0;
for (int i = 0; i < a.length(); i++) {
if (a[i] != b[i])
ans += 2;
}
int n = a.length();
// Traverse the strings.
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
if (a[i] == '0') {
// If Ai is not equal to Bi,
// increment the MaxSum by 2
if (i + 1 < n and a[i + 1] == '1'
and b[i + 1] == '1') {
ans += 2, i++;
}
// Else if the pair is only (0, 0)
// increment MaxSum by 1.
else {
ans += 1;
}
}
else {
if (i + 1 < n and a[i + 1] == '0'
and b[i + 1] == '0') {
ans += 2, i++;
}
else {
ans += 0;
}
}
}
}
return ans;
}
// Driver Code
int main()
{
string a = "0101000";
string b = "1101100";
cout << MaxSum(a, b);
return 0;
}
Java
// Java program to split two
// binary Strings in such a way
// such that sum is maximum.
import java.io.*;
public class GFG {
// Function to split Strings
// to find maximum sum
static int MaxSum(String a, String b) {
int ans = 0;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) != b.charAt(i))
ans += 2;
}
int n = a.length();
// Traverse the Strings.
for (int i = 0; i < n; i++) {
if (a.charAt(i) == b.charAt(i)) {
if (a.charAt(i) == '0') {
// If Ai is not equal to Bi,
// increment the MaxSum by 2
if (i + 1 < n && a.charAt(i + 1) == '1'
&& b.charAt(i + 1) == '1') {
ans += 2;
i++;
}
// Else if the pair is only (0, 0)
// increment MaxSum by 1.
else {
ans += 1;
}
} else {
if (i + 1 < n && a.charAt(i + 1) == '0'
&& b.charAt(i + 1) == '0') {
ans += 2;
i++;
} else {
ans += 0;
}
}
}
}
return ans;
}
// Driver Code
public static void main(String args[]) {
String a = "0101000";
String b = "1101100";
System.out.println(MaxSum(a, b));
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# python3 program to split two
# binary strings in such a way
# such that sum is maximum.
# Function to split strings
# to find maximum sum
def MaxSum(a, b):
ans = 0
for i in range(0, len(a)):
if (a[i] != b[i]):
ans += 2
n = len(a)
# Traverse the strings.
i = 0
while i < n:
if (a[i] == b[i]):
if (a[i] == '0'):
# If Ai is not equal to Bi,
# increment the MaxSum by 2
if (i + 1 < n and a[i + 1] == '1'
and b[i + 1] == '1'):
ans, i = ans + 2, i + 1
# Else if the pair is only (0, 0)
# increment MaxSum by 1.
else:
ans += 1
else:
if (i + 1 < n and a[i + 1] == '0'
and b[i + 1] == '0'):
ans, i = ans + 2, i + 1
else:
ans += 0
i += 1
return ans
# Driver Code
if __name__ == "__main__":
a = "0101000"
b = "1101100"
print(MaxSum(a, b))
# This code is contributed by rakeshsahni
C#
// C# program to split two
// binary strings in such a way
// such that sum is maximum.
using System;
class GFG
{
// Function to split strings
// to find maximum sum
static int MaxSum(string a, string b)
{
int ans = 0;
for (int i = 0; i < a.Length; i++) {
if (a[i] != b[i])
ans += 2;
}
int n = a.Length;
// Traverse the strings.
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
if (a[i] == '0') {
// If Ai is not equal to Bi,
// increment the MaxSum by 2
if (i + 1 < n && a[i + 1] == '1'
&& b[i + 1] == '1') {
ans += 2;
i++;
}
// Else if the pair is only (0, 0)
// increment MaxSum by 1.
else {
ans += 1;
}
}
else {
if (i + 1 < n && a[i + 1] == '0'
&& b[i + 1] == '0') {
ans += 2;
i++;
}
else {
ans += 0;
}
}
}
}
return ans;
}
// Driver Code
public static void Main()
{
string a = "0101000";
string b = "1101100";
Console.Write(MaxSum(a, b));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to split strings
// to find maximum sum
function MaxSum(a, b) {
let ans = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i])
ans += 2;
}
let n = a.length;
// Traverse the strings.
for (let i = 0; i < n; i++) {
if (a[i] == b[i]) {
if (a[i] == '0') {
// If Ai is not equal to Bi,
// increment the MaxSum by 2
if (i + 1 < n && a[i + 1] == '1'
&& b[i + 1] == '1') {
ans += 2, i++;
}
// Else if the pair is only (0, 0)
// increment MaxSum by 1.
else {
ans += 1;
}
}
else {
if (i + 1 < n && a[i + 1] == '0'
&& b[i + 1] == '0') {
ans += 2, i++;
}
else {
ans += 0;
}
}
}
}
return ans;
}
// Driver Code
let a = "0101000";
let b = "1101100";
document.write(MaxSum(a, b));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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:
6 min read
Maximize given function by selecting equal length substrings from given Binary Strings Given two binary strings s1 and s2. The task is to choose substring from s1 and s2 say sub1 and sub2 of equal length such that it maximizes the function: fun(s1, s2) = len(sub1) / (2xor(sub1, sub2)) Examples: Input: s1= "1101", s2= "1110"Output: 3Explanation: Below are the substrings chosen from s1
11 min read
Maximize sum of assigned weights by flipping at most K bits in given Binary String Given a binary string str of length N and an integer K, the task is to find the maximum possible sum of assigned weights that can be obtained by flipping at most K bits in the given binary string. The weight assigned to the characters of this string are as follows: If a character is '0', then the we
13 min read
Count of Binary Strings possible as per given conditions Given two integers N and M, where N denotes the count of '0' and M denotes the count of '1', and an integer K, the task is to find the maximum number of binary strings that can be generated of the following two types: A string can consist of K '0's and a single '1'.A string can consist of K '1's and
4 min read
Maximum string length after choosing strings from given Array with given conditions Given an array of string S[] of size N, the task is to find the maximum size of the resultant string formed by adding some strings and following the given condition that If a string of size K is chosen to add in the resultant string then the next K/2 strings cannot be selected to be a part of the re
9 min read
Maximum bitwise OR one of any two Substrings of given Binary String Given a binary string str, the task is to find the maximum possible OR value of any two substrings of the binary string str. Examples: Input: str = "1110010"Output: "1111110"Explanation: On choosing the substring "1110010" and "11100" we get the OR value as "1111110" which is the maximum value. Inpu
8 min read
Maximum splits in binary string such that each substring is divisible by given odd number Given binary string str, the task is to calculate the maximum possible splits possible to make each substring divisible by a given odd number K.Examples: Input: str = "110111001", K = 9 Output: 2 Explanation: The two possible substrings are "11011" and "1001". The equivalent decimal values are 27 an
5 min read
Maximize numbers that can be grouped together based on given conditions Given a 2D array A[][] of size N x 2 where: Every element lies between [1, N].A[i][0] signifies that there must be at most A[i][0] elements strictly lesser than i+1 and at most A[i][1] elements strictly greater than i+1. The task is to find the maximum number of elements that can come together abidi
10 min read
Maximize minority character deletions that can be done from given Binary String substring Python Given binary string str of size, N. Select any substring from the string and remove all the occurrences of the minority character (i.e. the character having less frequency) from the substring. The task is to find out the maximum number of characters that can be removed from performing one suc
5 min read
Maximize sum by picking Array element to left of each '1' of a Binary String Given a binary string S and an array arr[] each of size N, we can pick any element from the Array which is to the left of (or at the same position) the indices of '1's in the given binary string. The task is to find the maximum possible sum. Examples: Input: arr[] = {20, 10, 30, 9, 20, 9}, string S
5 min read