Skip to content

Commit ca4ccf0

Browse files
authored
Create KaprekarsConstant.java
1 parent 569d498 commit ca4ccf0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Hard/KaprekarsConstant.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
class Main {
5+
6+
public static int count=0;
7+
8+
public static int KaprekarsConstant(int num) {
9+
10+
String other=Integer.toString(num);
11+
int []arr=new int[other.length()];
12+
13+
for(int i=0;i<other.length();i++){
14+
arr[i]=other.charAt(i)-'0';
15+
}
16+
Arrays.sort(arr);
17+
18+
String low="";
19+
for(int i=0;i<arr.length;i++){
20+
low+=arr[i];
21+
}
22+
23+
String high=new StringBuilder(low).reverse().toString();
24+
int diff=Integer.parseInt(high)-Integer.parseInt(low);
25+
26+
if(diff<1000){
27+
diff=Integer.parseInt(Integer.toString(diff)+"0");
28+
}
29+
count++;
30+
return diff!=6174 ? KaprekarsConstant(diff) : count;
31+
}
32+
33+
public static void main (String[] args) {
34+
Scanner s = new Scanner(System.in);
35+
System.out.print(KaprekarsConstant(s.nextLine()));
36+
}
37+
}

0 commit comments

Comments
 (0)