Maximize the Binary String value by replacing A[i]th elements from start or end
Last Updated :
06 Sep, 2022
Given a string S of size M consisting of only zeroes (and hence representing the integer 0). Also, given an array A[] of size N whose, each element is an integer in the range [1, M]. The task is to maximize the integer represented by the string by performing the following operation N times :
- In ith (1 ? i ? N) operation, replace either A[i]th term or (M+1-A[i])th term with 1.
- Characters at the same position can be changed more than once.
Examples :
Input: N = 4, M = 5, S = "00000", A = {1, 1, 3, 1}
Output: "10101"
Explanation: In 1st operation, the element at 1st position (0-th index)
is transformed into 1.
In 2nd operation, since element at 1st position is already 1,
so make element at 5th position equal to 1.
In 3rd operation, make element at 3rd position equal to 1.
In 4th operation, we can make either element at 1st
or 5th position equal to 1. Both of them are already 1.
Input : N=1, M=5, S="00000", A={2}
Output : "01000"
Approach :
The problem can be solved easily by a greedy approach. The catch here is that to maximize the integer represented by the string, we must try to convert the 0s to 1s which are on as left as possible i.e., nearest to the most significant bit.
The following steps can be taken to solve this problem:
- Iterate through the elements of A[].
- For convenience, make A[i] = min(A[i], M+1-A[i]).
- To handle the 0-indexing of the array, M-1-A[i] would be written instead of M+1-A[i].
- If the A[i]th character of S is not 1, we will replace it.
- Else, we'll replace the (M+1-A[i])th character.
- Return the final string as the required answer.
Following is the code based on the above approach :
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to maximize the integer represented by
// the string by performing the given operations
string maxInteger(int N, int M, string S, int A[])
{
int i = 0;
// Loop to perform N operations
while (N--) {
// To handle 0-indexing
A[i]--;
// Initializing A[i] as the minimum
// of A[i] and M-1-A[i]
A[i] = min(A[i], M - 1 - A[i]);
// If element at the A[i] position of S
// is not 1 make it 1
if (S[A[i]] != '1') {
S[A[i]] = '1';
}
// Else make the element at M-1-A[i]th position 1
else {
S[M - 1 - A[i]] = '1';
}
i++;
}
// Returning maximized string after N operations
return S;
}
// Driver Code
int main()
{
int N = 4, M = 5;
string S = "00000";
int A[4] = { 1, 1, 3, 1 };
// Function call
string ans = maxInteger(N, M, S, A);
cout << ans << endl;
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to maximize the integer represented by
// the string by performing the given operations
static String maxInteger(int N, int M, String S, int A[])
{
int i = 0;
char str[] = S.toCharArray();
// Loop to perform N operations
while (N-->0) {
// To handle 0-indexing
A[i]--;
// Initializing A[i] as the minimum
// of A[i] and M-1-A[i]
A[i] = Math.min(A[i], M - 1 - A[i]);
// If element at the A[i] position of S
// is not 1 make it 1
if (str[A[i]] != '1') {
str[A[i]] = '1';
}
// Else make the element at M-1-A[i]th position 1
else {
str[M - 1 - A[i]] = '1';
}
i++;
}
// Returning maximized string after N operations
return new String(str);
}
public static void main (String[] args) {
int N = 4, M = 5;
String S = "00000";
int A[] = { 1, 1, 3, 1 };
// Function call
String ans = maxInteger(N, M, S, A);
System.out.println(ans);
}
}
// This code is contributed by aadityapburujwale
Python3
# python code to implement the approach
# Function to maximize the integer represented by
# the string by performing the given operations
def maxInteger(N, M, S, A):
i = 0
# Loop to perform N operations
while (N):
# To handle 0-indexing
A[i] -= 1
# Initializing A[i] as the minimum
# of A[i] and M-1-A[i]
A[i] = min(A[i], M - 1 - A[i])
# If element at the A[i] position of S
# is not 1 make it 1
if (S[A[i]] != '1'):
S[A[i]] = '1'
# Else make the element at M-1-A[i]th position 1
else:
S[M - 1 - A[i]] = '1'
i += 1
N -= 1
# Returning maximized string after N operations
return "".join(S)
# Driver Code
if __name__ == "__main__":
N = 4
M = 5
S = "00000"
A = [1, 1, 3, 1]
# Function call
ans = maxInteger(N, M, list(S), A)
print(ans)
# This code is contributed by rakeshsahni
C#
// Include namespace system
using System;
public class GFG
{
// Function to maximize the integer represented by
// the string by performing the given operations
public static String maxInteger(int N, int M, String S, int[] A)
{
var i = 0;
char[] str = S.ToCharArray();
// Loop to perform N operations
while (N-- > 0)
{
// To handle 0-indexing
A[i]--;
// Initializing A[i] as the minimum
// of A[i] and M-1-A[i]
A[i] = Math.Min(A[i],M - 1 - A[i]);
// If element at the A[i] position of S
// is not 1 make it 1
if (str[A[i]] != '1')
{
str[A[i]] = '1';
}
else
{
str[M - 1 - A[i]] = '1';
}
i++;
}
// Returning maximized string after N operations
return new String(str);
}
public static void Main(String[] args)
{
var N = 4;
var M = 5;
var S = "00000";
int[] A = {1, 1, 3, 1};
// Function call
var ans = GFG.maxInteger(N, M, S, A);
Console.WriteLine(ans);
}
}
// This code is contributed by aadityapburujwale.
JavaScript
<script>
// Javascript program for above approach
// Function to maximize the integer represented by
// the string by performing the given operations
function maxInteger(N, M, S, A)
{
let i = 0;
let str = S.split('');
// Loop to perform N operations
while (N-->0) {
// To handle 0-indexing
A[i]--;
// Initializing A[i] as the minimum
// of A[i] and M-1-A[i]
A[i] = Math.min(A[i], M - 1 - A[i]);
// If element at the A[i] position of S
// is not 1 make it 1
if (str[A[i]] != '1') {
str[A[i]] = '1';
}
// Else make the element at M-1-A[i]th position 1
else {
str[M - 1 - A[i]] = '1';
}
i++;
}
// Returning maximized string after N operations
return new String(str);
}
// Driver Code
let N = 4, M = 5;
let S = "00000";
let A = [ 1, 1, 3, 1 ];
// Function call
let ans = maxInteger(N, M, S, A);
document.write(ans);
// This code is contributed by code_hunt.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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
Find an N-length Binary String having maximum sum of elements from given ranges Given an array of pairs ranges[] of size M and an integer N, the task is to find a binary string of length N such that the sum of elements of the string from the given ranges is maximum possible. Examples: Input: N = 5, M = 3, ranges[] = {{1, 3}, {2, 4}, {2, 5}}Output: 01100Explanation:Range [1, 3]:
5 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
Minimize the maximum of 0s left or 1s deleted from Binary String You are given a binary string S of size N consisting of characters 0 and/or 1. You may remove several characters from the beginning or the end of the string. The task is to find the cost of removal where the cost is the maximum of the following two values: The number of characters 0 left in the stri
8 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
Minimize removal from front or end to make the Binary String at equilibrium Given a binary string S of size N, the task is to find the minimum number of removal required either from the start or end position from the binary strings such that the count of '0' and '1' becomes equal in the final string after removal. Examples: Input: S = "0111010"Output: 3Explanation: Remove 3
7 min read