Why am I getting TLE for input size 10? (I have used Backtracking)


#1

public class Solution {
ArrayList<ArrayList> ans= new ArrayList<ArrayList>();

public ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> A) {
    ans.add(new ArrayList<Integer>());
    Collections.sort(A);
    sub(A,ans);
        Collections.sort(ans, new Comparator<ArrayList<Integer>>() {
            @Override
            public int compare(ArrayList<Integer> first, ArrayList<Integer> second) {
                int comp = 0;
                for(int i = 0; i < Math.min(first.size(), second.size()); i++){
                    comp = Integer.compare(first.get(i), second.get(i));
                    if(comp != 0){
                        return comp;
                    }
                }
                return Integer.compare(first.size(), second.size());
            }
        });
    return ans;
}
static void sub(ArrayList<Integer> A, ArrayList<ArrayList<Integer>> ans)
{
    if(A.size()==1)
    {
        if(!ans.contains(A))
        ans.add(new ArrayList(A));
        return;
    }
    for(int i=A.size()-1;i>=0;i--)
    {
        int temp=A.remove(i);
        sub(A,ans);
        A.add(i,temp);
        if(!ans.contains(A))
        ans.add(new ArrayList(A));            
    }
}

}