Why Is Order Mandatory in this Question. Below is my code and method varies but answer is correct


#1

public ArrayList grayCode(int a) {
ArrayList r = new ArrayList<>();
helper(a,"",r);
return r;
}

private static void helper(int n, String s, ArrayList<Integer> r) {
    //System.out.println("TEMP :"+s);
    
    if(s.length()==n){
        //System.out.println("ADD :"+s);
        r.add(Integer.parseInt(s,2));
    } else{
        if(s.length()<n){
            helper(n,"0"+s,r);
            helper(n,"1"+s,r);
        }
    }
}

#2

Gray Code is all about order.
You refer to WIkipedia page for Gray Code.