import java.util.*;

/**
 * Generate gray code sequence of given length
 *
 * The reflected binary code, also known as Gray code after Frank Gray,
 *  is a binary numeral system where two successive values differ in only one bit (binary digit).
 *
 * Refer : http://e...content-available-to-author-only...a.org/wiki/Gray_code
 *
 */
class Ideone{

	public static void main(String[] args){
		int length = 4;
		List<String> grayCodeSeq = new ArrayList<String>(2);
		grayCodeSeq.add("0");
		grayCodeSeq.add("1");
		
		for(int i=1; i<length; i++){
		  grayCodeSeq = generateNextSequence(grayCodeSeq);
		}

		printList(grayCodeSeq);
	}

	private static void printList(List<String> list){
		for(int i=0; i<list.size(); i++){
			System.out.println(list.get(i));
		}
	}

	/**
	 * Generates gray code of next length sequeue
	 */
	private static List<String> generateNextSequence(List<String> grayCodes){
		if(grayCodes == null || grayCodes.isEmpty()){
			return grayCodes;
		}
		
		List<String> newSeq = new ArrayList<String>(grayCodes.size());
		for(int i=0; i<grayCodes.size();i++){
			newSeq.add("0"+grayCodes.get(i));
		}

		//Reverse the list 
		Collections.reverse(grayCodes);
		for(int i=0; i<grayCodes.size();i++){
			newSeq.add("1"+grayCodes.get(i));
		}
		return newSeq;
	}
}
