Maximum by Single Digit Replacement

Maximum by Single Digit Replacement  

Problem:

Two integers M and N are passed as the input to the program. The program must print the maximum value of M obtained by replacing exactly one digit in M by a digit from N. 

Boundary Condition(s): 

1 <= Number of digits in M <= 100 

1 <= Number of digits in N <= 10 

Input Format: 

The first line contains M and N separated by space(s). 

Output Format:

The first line contains the maximum value of M. 

Example Input/Output 1: 

Input: 

56120 21 

Output: 

56220 

Explanation: 

The maximum value is obtained by replacing 1 in 56120 by 2. Any other replacements would give smaller values. 

Example Input/Output 2: 

Input: 

895496223 5 

Output: 

895596223 

Program:

import java.util.*;

public class Hello {


    public static void main(String[] args) 

    {

Scanner sc = new Scanner(System.in); 

String m = sc.next(); 

String n = sc.next();

char max = '0';

for(int i=0;i<n.length();i++)

{

    if(n.charAt(i) > max)

    {

        max = n.charAt(i);

    }

}

String ans = "";

int c = 0;

for(int i=0;i<m.length();i++)

{

    if(m.charAt(i) < max && c==0)

    {

        ans += max;

        c = 1;

    }

    else

    {

        ans += m.charAt(i);

    }

}

System.out.print(ans);

}

}

Comments

Popular posts from this blog

Pronic Integers in N - InfyTQ question

Count Strong Points - Accenture

Letters at position of multiples of a number