method to Generate Binary Numbers from 1 to n with running time O(nlogn)