No, you can't append 0 as prefix to an integer. An integer starting with 0 is assumed as octal representation of corresponding decimal integer, i.e.: (037)8 == (31)10 If you really want to save the numbers with prefixed 0s, you will have to store the numbers as strings. You can...

Wrote the following and figured I'd share it as I don't see many Java implementations showing up on here: static String getGreyCode(int myNum, int numOfBits) { if (numOfBits == 1) { return String.valueOf(myNum); } if (myNum >= Math.pow(2, (numOfBits - 1))) { return "1" + getGreyCode((int)(Math.pow(2, (numOfBits))) - myNum -...

algorithm,sorting,combinations,combinatorics,gray-code

While I really appreciate the efforts from @tucuxi and @David Eisenstadt, I found some issues with the Hamiltonian approach, namely that it didn't solve for certain values of n and k and certain elements were also unnecessarily discriminated. I decided to give the ideas listed in my question a go...

java,python,c,algorithm,gray-code

I've had to solve this question in an interview as well. One of the conditions for the two values to be a gray code sequence is that their values only differ by 1 bit. Here is a solution to this problem: def isGrayCode(num1, num2): differences = 0 while (num1 >...