forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiggerIsGreater.java
More file actions
46 lines (41 loc) · 1.1 KB
/
BiggerIsGreater.java
File metadata and controls
46 lines (41 loc) · 1.1 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
package Implementation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* @author -- rajatgoyal715
*/
public class BiggerIsGreater {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
String s=br.readLine();
char arr[]=s.toCharArray();
if(!hasNext(arr))
System.out.println("no answer");
}
}
public static boolean hasNext(char arr[]){
int n=arr.length;
int i,j;
for(i=n-2;i>=0;i--)
if(arr[i]<arr[i+1])
break;
if(i==-1)
return false;
j=n-1;
while(arr[i]>=arr[j])
j--;
char temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
for(int r=n-1,s=i+1;r>s;r--,s++){
temp=arr[r];
arr[r]=arr[s];
arr[s]=temp;
}
System.out.println(arr);
return true;
}
}