Stack Overflow On Java, on line when recursively all grey codes of current index 1 is retrieved


#1

Code goes like –
public class Solution {
public ArrayList sol = new ArrayList();
public boolean nperm(ArrayList a, int curr){
if (curr==a.size()-3){
String pp = “”;
for(int i=0;i<a.size();i++){
pp = pp+Integer.toString(a.get(i));
}
sol.add(Integer.parseInt(pp));
a.set(curr+1,1-a.get(curr+1));
pp = “”;
for(int i=0;i<a.size();i++){
pp = pp+Integer.toString(a.get(i));
}
sol.add(Integer.parseInt(pp));
a.set(curr+2,1-a.get(curr+2));
pp = “”;
for(int i=0;i<a.size();i++){
pp = pp+Integer.toString(a.get(i));
}
sol.add(Integer.parseInt(pp));
a.set(curr+1,1-a.get(curr+1));
pp = “”;
for(int i=0;i<a.size();i++){
pp = pp+Integer.toString(a.get(i));
}
sol.add(Integer.parseInt(pp));
}
else if (curr<a.size()-3){
nperm(a,curr++);
a.set(curr,1-a.get(curr));
nperm(a,curr++);
}
else{
return true;
}
return true;
}
public ArrayList grayCode(int a) {
ArrayList A = new ArrayList();
for(int i=0;i<a;i++){
A.add(0);
}

    nperm(A,0);
    return sol;
}

}