Maximize value of Binary String in K steps by performing XOR of Substrings
Last Updated :
27 Apr, 2023
Given a binary string S of size N, the task is to find the maximum possible value in exactly K step, where in each step, perform XOR of two substrings of S (possibly intersecting, possibly the same, possibly non-intersecting).
Examples:
Input: string S ="1010010", K = 2
Output: "1111110"
Explanation:
In the first step, choose one substring as 1010010 and another as 101001, the XOR of these both strings will be 1111011.
In the second step, Choose one substring as 1111011 and another as 101, the XOR of these both strings will be 1111110, which is the maximum possible value you can get in K steps.
Input: string S ="11010", K = 1
Output: "11111"
Explanation: Choose one substring as 11010 and another substring as 101, the XOR of these both strings will be 11111, which is the maximum possible value you can get in one step.
Approach: The given problem can be solved by using the below idea.
Choose one substring as String S because it has maximum possible decimal value and other as the size - index of leftmost 0 because we have to maximise the size of substring and convert 0 to 1 and do this step K times.
Follow the steps to solve this problem:
- Count the number of 1s and 0s in the string.
- If the count of 1s is equal to the length of the string, then there can be two cases
- If K is a multiple of 2, i.e., even we can pick a substring '1' and XOR it even times, so the string will remain unchanged.
- Otherwise, we will pick a substring '1' and XOR it, so only the least significant bit will be changed to '0'.
- If the count of 0s is equal to the length of the string, return 0 as no 1 is there.
- Take one substring as S and another (say t) is any possible substring of size size N-idx where idx is the index of the leftmost 0.
- Take XOR of S and t, this is the maximum possible value.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// Function to insert n 0s in the
// beginning of the given string
void addZeros(string& str, int n)
{
for (int i = 0; i < n; i++) {
str = "0" + str;
}
}
// Function to return the XOR
// of the given strings
string getXOR(string a, string b)
{
// Lengths of the given strings
int aLen = a.length();
int bLen = b.length();
// Make both the strings of equal lengths
// by inserting 0s in the beginning
if (aLen > bLen) {
addZeros(b, aLen - bLen);
}
else if (bLen > aLen) {
addZeros(a, bLen - aLen);
}
// Updated length
int len = max(aLen, bLen);
// To store the resultant XOR
string res = "";
for (int i = 0; i < len; i++) {
if (a[i] == b[i])
res += "0";
else
res += "1";
}
return res;
}
// Function to find max Xor of one string
string maxValue(string S)
{
// Initialize variables
ll n = S.size();
// Flag to get index of leftmost
// zero
bool f = 1;
ll Idx, Count0 = 0, Count1 = 0;
// Count the number of 1's and 0's in
// the string and also index of left
// most 0 in the string
for (ll i = 0; i < n; i++) {
// Character is equal to 1
if (S[i] == '1') {
Count1++;
}
// Character is equal to 0
else {
if (f) {
// Storing the index of
// leftmost zero
Idx = i;
f = 0;
}
Count0++;
}
}
// If all are 1's in the string
if (Count1 == n) {
return getXOR(S, "1");
}
// If all are 0's in the string
// print 0
if (Count0 == n) {
return "0";
}
// Find another substring
string t = S.substr(Idx, n - Idx);
// Find it's size
ll size = t.size();
string dec = t;
string Max = "";
// First and second substring
string t1 = "", t2 = "";
// Find second substring
for (ll j = 0; j < n; j++) {
if (j < size) {
t1 += S[j];
}
else {
string dec1 = t1;
// Function to calculate XOR of
// two string
string res = getXOR(dec, dec1);
if (res > Max) {
Max = res;
t2 = t1;
}
t1 = t1.substr(1);
t1 += S[j];
}
}
string dec1 = t1;
string res = getXOR(dec1, dec);
if (res > Max) {
Max = res;
t2 = t;
}
// First and second string
string d1 = S;
string d2 = t2;
// Xor of first and second substring
string ans = getXOR(d1, d2);
Idx = -1;
// Remove leading zeroes
for (ll i = 0; i < ans.size(); i++) {
if (ans[i] != '0') {
Idx = i;
break;
}
}
if (Idx == -1) {
return "0";
}
// Return resultant substring
return ans.substr(Idx);
}
// Function to calculate maximum XOR value
// in exactly K steps
string solve(string s, int K)
{
int i = 0;
while (i < K) {
// Function call to find maximum
// XOR value of s
s = maxValue(s);
i++;
}
return s;
}
// Driver Code
int main()
{
string s = "1010010";
int K = 2;
// Function call
cout << solve(s, K) << endl;
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to return the XOR
// of the given strings
static String getXOR(String a, String b)
{
// Lengths of the given strings
int aLen = a.length();
int bLen = b.length();
// Make both the strings of equal lengths
// by inserting 0s in the beginning
if (aLen > bLen) {
b = addZeros(b, aLen - bLen);
}
else if (bLen > aLen) {
a = addZeros(a, bLen - aLen);
}
// Updated length
int len = Math.max(aLen, bLen);
// To store the resultant XOR
String res = "";
for (int i = 0; i < len; i++) {
if (a.charAt(i) == b.charAt(i))
res += "0";
else
res += "1";
}
return res;
}
// Function to insert n 0s in the
// beginning of the given string
static String addZeros(String s, int numZeros)
{
for (int i = 0; i < numZeros; i++) {
s = "0" + s;
}
return s;
}
// Function to find max Xor of one string
static String maxValue(String S)
{
// Initialize variables
int n = S.length();
// Flag to get index of leftmost
// zero
boolean f = true;
int Idx = 0, Count0 = 0, Count1 = 0;
// Count the number of 1's and 0's in
// the string and also index of left
// most 0 in the string
for (int i = 0; i < n; i++) {
// Character is equal to 1
if (S.charAt(i) == '1') {
Count1++;
}
// Character is equal to 0
else {
if (f) {
// Storing the index of
// leftmost zero
Idx = i;
f = false;
}
Count0++;
}
}
// If all are 1's in the string
if (Count1 == n) {
return getXOR(S, "1");
}
// If all are 0's in the string
// print 0
if (Count0 == n) {
return "0";
}
// Find another substring
String t = S.substring(Idx, n);
// Find it's size
int size = t.length();
String dec = t;
String Max = "";
// First and second substring
String t1 = "", t2 = "";
// Find second substring
for (int j = 0; j < n; j++) {
if (j < size) {
t1 += S.charAt(j);
}
else {
String dec1 = t1;
// Function to calculate XOR of
// two string
String res = getXOR(dec, dec1);
if (res.compareTo(Max) > 0) {
Max = res;
t2 = t1;
}
t1 = t1.substring(1);
t1 += S.charAt(j);
}
}
String dec1 = t1;
String res = getXOR(dec1, dec);
if (res.compareTo(Max) > 0) {
Max = res;
t2 = t;
}
// First and second string
String d1 = S;
String d2 = t2;
// Xor of first and second substring
String ans = getXOR(d1, d2);
Idx = -1;
// Remove leading zeroes
for (int i = 0; i < ans.length(); i++) {
if (ans.charAt(i) != '0') {
Idx = i;
break;
}
}
if (Idx == -1) {
return "0";
}
// Return resultant substring
return ans.substring(Idx);
}
// Function to calculate maximum XOR value
// in exactly K steps
static String solve(String s, int K)
{
int i = 0;
while (i < K) {
// Function call to find maximum XOR value of s
s = maxValue(s);
i++;
}
return s;
}
public static void main(String[] args)
{
String s = "1010010";
int K = 2;
// Function call
System.out.println(solve(s, K));
}
}
// This code is contributed by lokesh.
Python3
# python code
def add_zeros(s: str, n: int) -> str:
return "0" * n + s
def get_xor(a: str, b: str) -> str:
a_len = len(a)
b_len = len(b)
if a_len > b_len:
b = add_zeros(b, a_len - b_len)
elif b_len > a_len:
a = add_zeros(a, b_len - a_len)
len_ = max(a_len, b_len)
res = ""
for i in range(len_):
if a[i] == b[i]:
res += "0"
else:
res += "1"
return res
def maxValue(S: str) -> str:
n = len(S)
f = True
Idx = 0
Count0 = 0
Count1 = 0
for i in range(n):
if S[i] == "1":
Count1 += 1
else:
if f:
Idx = i
f = False
Count0 += 1
if Count1 == n:
return get_xor(S, "1")
if Count0 == n:
return "0"
t = S[Idx:]
size = len(t)
dec = t
Max = ""
t1 = ""
t2 = ""
for j in range(n):
if j < size:
t1 += S[j]
else:
dec1 = t1
res = get_xor(dec, dec1)
if res > Max:
Max = res
t2 = t1
t1 = t1[1:]
t1 += S[j]
dec1 = t1
res = get_xor(dec1, dec)
if res > Max:
Max = res
t2 = t
d1 = S
d2 = t2
ans = get_xor(d1, d2)
Idx = -1
for i in range(len(ans)):
if ans[i] != "0":
Idx = i
break
if Idx == -1:
return "0"
return ans[Idx:]
def solve(s, K):
i = 0
while (i < K):
s = maxValue(s)
i += 1
return s
s = "1010010"
K = 2
print(solve(s, K))
# This code is contributed by ksam24000
C#
// C# code to implement the approach
using System;
public class GFG {
// Function to return the XOR
// of the given strings
static string getXOR(string a, string b)
{
// Lengths of the given strings
int aLen = a.Length;
int bLen = b.Length;
// Make both the strings of equal lengths
// by inserting 0s in the beginning
if (aLen > bLen) {
b = AddZeros(b, aLen - bLen);
}
else if (bLen > aLen) {
a = AddZeros(a, bLen - aLen);
}
// Updated length
int len = Math.Max(aLen, bLen);
// To store the resultant XOR
string res = "";
for (int i = 0; i < len; i++) {
if (a[i] == b[i])
res += "0";
else
res += "1";
}
return res;
}
// Function to insert n 0s in the
// beginning of the given string
static string AddZeros(string s, int numZeros)
{
for (int i = 0; i < numZeros; i++) {
s = "0" + s;
}
return s;
}
// Function to find max Xor of one string
static string maxValue(string S)
{
// Initialize variables
int n = S.Length;
// Flag to get index of leftmost
// zero
bool f = true;
int Idx = 0, Count0 = 0, Count1 = 0;
// Count the number of 1's and 0's in
// the string and also index of left
// most 0 in the string
for (int i = 0; i < n; i++) {
// Character is equal to 1
if (S[i] == '1') {
Count1++;
}
// Character is equal to 0
else {
if (f) {
// Storing the index of
// leftmost zero
Idx = i;
f = false;
}
Count0++;
}
}
// If all are 1's in the string
if (Count1 == n) {
return getXOR(S, "1");
}
// If all are 0's in the string
// print 0
if (Count0 == n) {
return "0";
}
// Find another substring
string t = S.Substring((int)Idx, (int)(n - Idx));
// Find it's size
int size = t.Length;
string dec = t;
string Max = "";
// First and second substring
string t1 = "", t2 = "";
// Find second substring
for (int j = 0; j < n; j++) {
if (j < size) {
t1 += S[j];
}
else {
string dec1 = t1;
// Function to calculate XOR of
// two string
string res = getXOR(dec, dec1);
if (res.CompareTo(Max) > 0) {
Max = res;
t2 = t1;
}
t1 = t1.Substring(1);
t1 += S[j];
}
}
string temp1 = t1;
string temp2 = getXOR(temp1, dec);
if (temp2.CompareTo(Max) > 0) {
Max = temp2;
t2 = t;
}
// First and second string
string d1 = S;
string d2 = t2;
// Xor of first and second substring
string ans = getXOR(d1, d2);
Idx = -1;
// Remove leading zeroes
for (int i = 0; i < ans.Length; i++) {
if (ans[i] != '0') {
Idx = i;
break;
}
}
if (Idx == -1) {
return "0";
}
// Return resultant substring
return ans.Substring(Idx);
}
// Function to calculate maximum XOR value
// in exactly K steps
static string solve(string s, int K)
{
int i = 0;
while (i < K) {
// Function call to find maximum XOR value of s
s = maxValue(s);
i++;
}
return s;
}
static public void Main()
{
// Code
string s = "1010010";
int K = 2;
// Function call
Console.WriteLine(solve(s, K));
}
}
// This code is contributed by lokeshmvs21.
JavaScript
// JavaScript code to implement the approach
// Function to insert n 0s in the
// beginning of the given string
const addZeros = (str, n) => {
for (let i = 0; i < n; i++) {
str = "0" + str;
}
return str;
}
// Function to return the XOR
// of the given strings
const getXOR = (a, b) => {
// Lengths of the given strings
let aLen = a.length;
let bLen = b.length;
// Make both the strings of equal lengths
// by inserting 0s in the beginning
if (aLen > bLen) {
b = addZeros(b, aLen - bLen);
}
else if (bLen > aLen) {
a = addZeros(a, bLen - aLen);
}
// Updated length
let len = Math.max(aLen, bLen);
// To store the resultant XOR
let res = "";
for (let i = 0; i < len; i++) {
if (a[i] == b[i])
res += "0";
else
res += "1";
}
return res;
}
// Function to find max Xor of one string
const maxValue = (S) => {
// Initialize variables
let n = S.length;
// Flag to get index of leftmost
// zero
let f = 1;
let Idx, Count0 = 0, Count1 = 0;
// Count the number of 1's and 0's in
// the string and also index of left
// most 0 in the string
for (let i = 0; i < n; i++) {
// Character is equal to 1
if (S[i] == '1') {
Count1++;
}
// Character is equal to 0
else {
if (f) {
// Storing the index of
// leftmost zero
Idx = i;
f = 0;
}
Count0++;
}
}
// If all are 1's in the string
if (Count1 == n) {
return getXOR(S, "1");
}
// If all are 0's in the string
// print 0
if (Count0 == n) {
return "0";
}
// Find another substring
let t = S.substring(Idx, n);
// Find it's size
let size = t.length;
let dec = t;
let Max = "";
// First and second substring
let t1 = "", t2 = "";
// Find second substring
for (let j = 0; j < n; j++) {
if (j < size) {
t1 += S[j];
}
else {
let dec1 = t1;
// Function to calculate XOR of
// two string
let res = getXOR(dec, dec1);
if (res > Max) {
Max = res;
t2 = t1;
}
t1 = t1.substring(1);
t1 += S[j];
}
}
let dec1 = t1;
let res = getXOR(dec1, dec);
if (res > Max) {
Max = res;
t2 = t;
}
// First and second string
let d1 = S;
let d2 = t2;
// Xor of first and second substring
let ans = getXOR(d1, d2);
Idx = -1;
// Remove leading zeroes
for (let i = 0; i < ans.length; i++) {
if (ans[i] != '0') {
Idx = i;
break;
}
}
if (Idx == -1) {
return "0";
}
// Return resultant substring
return ans.substring(Idx);
}
// Function to calculate maximum XOR value
// in exactly K steps
const solve = (s, K) => {
let i = 0;
while (i < K) {
// Function call to find maximum
// XOR value of s
s = maxValue(s);
i++;
}
return s;
}
// Driver Code
let s = "1010010";
let K = 2;
// Function call
console.log(solve(s, K));
// This code is contributed by rakeshsahni
Time Complexity: O(N2 * K)
Auxiliary Space: O(N)
Related Articles:
Similar Reads
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
Rearrange given binary strings to maximize their Bitwise XOR value Given three binary strings S1, S2, and S3 each of length N, the task is to find the maximum possible Bitwise XOR that can be obtained by rearranging the characters of the given strings. Examples: Input: S1 = "1001", S2 = "0010", S3 = "1110"Output: 15Explanation:Rearrange the digits of S1 as "1010",
15+ min read
Maximum number of set bits count in a K-size substring of a Binary String Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't
10 min read
Minimize hamming distance in Binary String by setting only one K size substring bits Given two binary strings S and T of length N and a positive integer K. Initially, all characters of T are '0'. The task is to find the minimum Hamming distance after choosing a substring of size K and making all elements of string T as '1' only once. Examples: Input: S = "101", K = 2Output: 1Explana
7 min read
Maximize non-overlapping Substrings by splitting String into groups with K or (K+1) 1s Given a binary string S of length N and an integer K, the task is to find the maximum number of non-overlapping substrings that can be obtained by splitting it as per the following conditions: Each substring of the string contains K or K+1 number of 1. At most consecutive K substrings can contain th
10 min read