How can I promote binary array using recursion func. The function receives binary array V and increases the value of the number represented by V the following number with the same number of unity. Function returns true if the operation can be performed (java)
Example:
v = {0,0,0,1,1,0,0,1,1} => return true, v = {0,0,0,1,1,0,1,0,1}
i write this:
public static boolean incrementSameOnes(int[] vec) {
boolean succ=false;
int[] v=new int[vec.length-1];
if(vec.length==1){
return false;
}
if (vec[vec.length-1]==1 && vec[vec.length-2]==0)
{
vec[vec.length-2] = 1;
vec[vec.length-1] = 0;
System.out.print(Arrays.toString(vec));
return true;
}else {
for(int j=0;j<vec.length-1;j++)
v[j]=vec[j];
succ=incrementSameOnes(v);
}
return succ;
}