I can't find the bug or problem because my code works for the test cases, and I can't think of any other ones.
any feedback to put me in the right direction would be greatly appreciated! thank you!
import java.util.*;
class JNEXT {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
StringBuilder reform = new StringBuilder();
int testCases = input.nextInt();
for (int T = testCases; T > 0; T--) {
int[] size = new int[input.nextInt()];
for (int i = 0; i < size.length; i++) {
size[i] = input.nextInt();
}
if (size.length < 2) {
System.out.println(-1);
break;
}
// variables to help with swapping
int first = -1;
int second = -1;
int spot = 0;
// for loop finding initial condition of k < k + 1
for (int k = 0; k + 1 < size.length; k++) {
if (size[k] < size[k + 1]) {
first = k;
}
}
// finding largest index that K < L
if (first != -1) {
for (int L = first + 1; L < size.length; L++) {
if (size[first] < size[L])
second = L;
}
// swapping K and L
spot = size[first];
size[first] = size[second];
size[second] = spot;
//reversing the numbers after size[first] by swapping
int temp = 0;
for (int start = first + 1, end = size.length - 1; start <= end ; start++, end--) {
temp = size[start];
size[start] = size[end];
size[end] = temp;
}
}
if (first == -1 || second == -1) {
reform.append(-1);
reform.append("\n");
}
else {
for(int i = 0; i < size.length; i++) {
reform.append(size[i]);
}
reform.append("\n");
}
}
System.out.print(reform + "");
}
}