-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindingMaxNumberBySwapping6And9.java
More file actions
57 lines (52 loc) · 1.26 KB
/
Copy pathFindingMaxNumberBySwapping6And9.java
File metadata and controls
57 lines (52 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// System.out.println("Hello World");
int n = sc.nextInt();
System.out.println(Main.solution(n));
}
public static int solution(int A){
char []B = String.valueOf(A).toCharArray();
int mx = A;
int six=0,nine=0;
//String x = String.valueOf(A);
for(int i=0;i<B.length;i++){
if(B[i]=='6'){
B[i]='9';
six=1;
String x = new String(B);
int y = Integer.parseInt(x);
//System.out.println("y"+y);
if(mx<y){
mx = y;
}
}
else if(B[i]=='9'){
B[i]='6';
nine=1;
String x = new String(B);
int y = Integer.parseInt(x);
System.out.println("y"+y);
if(mx<y){
mx = y;
}
}
else{
// do nothing!!
}
if(nine==1){
B[i]='9';
nine=0;
}
if(six==1){
B[i]='6';
six=0;
}
}
return mx;
}
}
// Example:
// 669 -----> 969,696,666----> max = 969.