write a functoin that given a three digit integer n and an integer
k return the maximum possible three digit value that can be obtained
by performing at most k increase by 1 of any digit in n
package maximumpossiblevalue;
public class MaximumPossibleValue {
public static int maximum(int num, int k){
String string = String.valueOf(num);
char first_character = string.charAt(0);
String a = String.valueOf(first_character);
int first = Integer.parseInt(a);
char second_character = string.charAt(1);
String b = String.valueOf(second_character);
int second = Integer.parseInt(b);
char third_character = string.charAt(2);
String c = String.valueOf(third_character);
int third = Integer.parseInt(c);
int first_temp = first;
for(int i=1; i<=k; i++){
first_temp++;
}
String first_max = String.valueOf(first_temp) + String.valueOf(second) + String.valueOf(third);
int second_temp = second;
for(int i=1; i<=k; i++){
second_temp++;
}
String second_max = String.valueOf(first) + String.valueOf(second_temp) + String.valueOf(third);
int third_temp = third;
for(int i=1; i<=k; i++){
third_temp++;
}
String third_max = String.valueOf(first) + String.valueOf(second) + String.valueOf(third_temp);
int first_possible= Integer.parseInt(first_max);
int second_possible= Integer.parseInt(second_max);
int third_possible= Integer.parseInt(third_max);
int [] arr= {first_possible,second_possible, third_possible};
int max = arr[0];
for(int i=0; i<3; i++){
if(arr[i]>max){
max = arr[0];
}
}
return max;
}
public static void main(String[] args) {
System.out.printf("\nMaximum possible three digit value %d\n", maximum(476,5));
}
}
Comments
Leave a comment