ranga.madhu
BAN USER
Comments (1)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ Algorithm
Approach 1 with more memory but optimal:
0. C for count of number of integers used.
1. initialize A array of integers in sequence from 0 to end.
2. Iniitalize B an array of bits for the same size n ( every bit is false) Each position in the bit array represents one element of integer if it is assigned then it is true else false.
if we need random number then third step else go to 4
3. {{{ For I=o to n:
rand1 = Generate a random number between to 0 to n.
rand 2 = Generate a random number between to 0 to n.
swap A[rand1] with A[Rand2]
4.
requestNumber (int value)
{
if value is between 0 to size of B array-1)
{
if(B[value])
return false;
else
B[A[C]] = true;
return true;
}
}
}
}}}
-
ranga.madhu
February 02, 2015 | Flag
Reply
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.