After several attempt trying and improving I came to this solution of linear time complexity. But my solution shows TLE when I submit it. Everything in solutions seems fine.
public static void main(String[] argh) throws Exception{
Reader br = new Reader();
int t= br.nextInt();
for(int aa=0; aa<t; aa++){
int n= br.nextInt();
int[] arr= new int[n];
int breakPoint= -1;
int mI= -1;
for(int qq=0; qq<n; qq++){
arr[qq]= br.nextInt();
if(qq !=0 && arr[qq] > arr[qq-1]){
breakPoint= qq-1;
}
if(breakPoint != -1){
if(arr[breakPoint] < arr[qq]){
mI= qq;
}
}
}
if(breakPoint == -1 || n == 1){
System.out.println(-1);
continue;
}
// swap
arr[breakPoint]= arr[breakPoint] + arr[mI];
arr[mI]= arr[breakPoint] - arr[mI];
arr[breakPoint]= arr[breakPoint] - arr[mI];
breakPoint++;
for(int ii=0; ii<breakPoint; ii++){
System.out.print(arr[ii]);
}
for(int ii=n-1; ii>=breakPoint; ii--){
System.out.print(arr[ii]);
}
System.out.println("");
}
}
Anyone please point out the issue.