What is wrong in this code. (Not giving right answer)?


#1

public class Solution {

public ArrayList<Integer> nextPermutation(ArrayList<Integer> A) {
    boolean temp = false;
    for(int j=A.size()-1;j>=0;j--){
        for(int i=j;i>=0;i--){
            if(A.get(i)<A.get(j)){
                A.add(i,A.remove(j));
                Collections.sort(A.subList(i+1,A.size()));
                temp = true;
                break;
            }
        }
        if(temp){
            break;
        }
    }
    if(!temp){
        Collections.sort(A);
    }
    return A;
}

}