Open In App

Maximize the Binary String value by replacing A[i]th elements from start or end

Last Updated : 06 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output

10101

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads