forked from Jasmine-21/Java-FOP
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnextlargestnum.java
More file actions
30 lines (29 loc) · 972 Bytes
/
nextlargestnum.java
File metadata and controls
30 lines (29 loc) · 972 Bytes
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
import java.util.*;
public class nextlargestnum {
public static void main(String args[]) {
String str;
Scanner s = new Scanner(System.in);
str = s.nextLine();
nextLargestNumber(str);
}
static void nextLargestNumber(String str) {
int n = str.length(); StringBuilder s = new StringBuilder(str);
int i = n - 1; while (i > 0 && s.charAt(i) <= s.charAt(i - 1))
i--;
int j = i - 1; if (j >= 0) {
int requiredIndex = i;
i++;
while (i < n && s.charAt(i) > s.charAt(j)) {
if (s.charAt(i) < s.charAt(requiredIndex))
requiredIndex = i;
i++;
}
char temp = s.charAt(j);
s.setCharAt(j, s.charAt(requiredIndex));
s.setCharAt(requiredIndex, temp);
System.out.println(s);
}
else
System.out.println("Not Possible");
}
}