forked from Nikky01/GeekForGeeks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Next_Greater_number.java
58 lines (51 loc) · 983 Bytes
/
Next_Greater_number.java
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
58
package com.geek.adobe;
import java.util.Arrays;
import java.util.Scanner;
public class Next_Greater_number {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc =new Scanner(System.in);
int t=sc.nextInt();
for(int j=0;j<t;j++)
{
String n=sc.next();
getNextGrtNumber(n);
System.out.println("");
}
}
private static void getNextGrtNumber(String n) {
// TODO Auto-generated method stub
char[] c=n.toCharArray();
int i;
for( i=c.length-1;i>0;i--)
{
if(c[i]>c[i-1])
{
break;
}
}
if(i==0)
{
System.out.println("Not Possible");
}
else
{
int x=c[i-1];
int min =i;
for(int j=i+1;j<c.length;j++)
{
if(c[j]>x&&c[j]<c[min])
{
min=j;
}
}
char t=c[i-1];
c[i-1]=c[min];
c[min]=t;
Arrays.sort(c, i, c.length);
for(char g:c)
System.out.print(g);
System.out.println("");
}
}
}