for(int i = q.length-1; i >= 0; i--){
if(q[i] != i+1){
try{
if(((i-1)>=0) & (q[i-1] == (i+1))){
steps++;
q[i-1] = q[i];
q[i] = i+1;
System.out.println("here "+i);
}
if(((i-2) >=0) & (q[i-2] == (i+1))){
q[i-2] = q[i-1];
q[i-1] = q[i];
q[i] = i+1;
steps += 2;
}else{
System.out.println("Too chaotic");
return;
}
}catch(Exception e){
System.out.println("error at the " +i + " exception" +e);
}
}
}
System.out.println(steps);
* The last value of i, I get in the catch is 1. I tried to print i in the if condition and it is printing the i value as 1 till the last line. Now, I don't understand where the exception is happening? *