I have on one random number generator which generates number between 1 to k
. Also i have array of int
type (i.e int[]
) ,whose size is N
, where k
is smaller than N .
Now problem is i need to save unique generated numbers into array (rejecting the generated duplicate number) and has to maintain order of generated numbers , without using any extra space and in O(N)
complexity. i.e i same array i need to maintain order of generated number also. So that i can retrieve them in generated order. No bitmap or extra array etc is suppose to use.
Its not a homework. Its one of the interview question. I am not supposed to use any extra space. He asked me to use the fact that k
is smaller than N
and you need to inculcate the behavior of hashmap in same array. I proposed many algorithms using extra spaces but he rejected also using sorting , but i was not able to maintain generated order.