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++
#include <bits/stdc++.h>
using namespace std;
string maxInteger( int N, int M, string S, int A[])
{
int i = 0;
while (N--) {
A[i]--;
A[i] = min(A[i], M - 1 - A[i]);
if (S[A[i]] != '1' ) {
S[A[i]] = '1' ;
}
else {
S[M - 1 - A[i]] = '1' ;
}
i++;
}
return S;
}
int main()
{
int N = 4, M = 5;
string S = "00000" ;
int A[4] = { 1, 1, 3, 1 };
string ans = maxInteger(N, M, S, A);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static String maxInteger( int N, int M, String S, int A[])
{
int i = 0 ;
char str[] = S.toCharArray();
while (N--> 0 ) {
A[i]--;
A[i] = Math.min(A[i], M - 1 - A[i]);
if (str[A[i]] != '1' ) {
str[A[i]] = '1' ;
}
else {
str[M - 1 - A[i]] = '1' ;
}
i++;
}
return new String(str);
}
public static void main (String[] args) {
int N = 4 , M = 5 ;
String S = "00000" ;
int A[] = { 1 , 1 , 3 , 1 };
String ans = maxInteger(N, M, S, A);
System.out.println(ans);
}
}
|
Python3
def maxInteger(N, M, S, A):
i = 0
while (N):
A[i] - = 1
A[i] = min (A[i], M - 1 - A[i])
if (S[A[i]] ! = '1' ):
S[A[i]] = '1'
else :
S[M - 1 - A[i]] = '1'
i + = 1
N - = 1
return "".join(S)
if __name__ = = "__main__" :
N = 4
M = 5
S = "00000"
A = [ 1 , 1 , 3 , 1 ]
ans = maxInteger(N, M, list (S), A)
print (ans)
|
C#
using System;
public class GFG
{
public static String maxInteger( int N, int M, String S, int [] A)
{
var i = 0;
char [] str = S.ToCharArray();
while (N-- > 0)
{
A[i]--;
A[i] = Math.Min(A[i],M - 1 - A[i]);
if (str[A[i]] != '1' )
{
str[A[i]] = '1' ;
}
else
{
str[M - 1 - A[i]] = '1' ;
}
i++;
}
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};
var ans = GFG.maxInteger(N, M, S, A);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function maxInteger(N, M, S, A)
{
let i = 0;
let str = S.split( '' );
while (N-->0) {
A[i]--;
A[i] = Math.min(A[i], M - 1 - A[i]);
if (str[A[i]] != '1' ) {
str[A[i]] = '1' ;
}
else {
str[M - 1 - A[i]] = '1' ;
}
i++;
}
return new String(str);
}
let N = 4, M = 5;
let S = "00000" ;
let A = [ 1, 1, 3, 1 ];
let ans = maxInteger(N, M, S, A);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)