GeeksforGeeks » Interview Questions
sort N decks of 52 playing cards
(3 posts)-
Write a function to sort N decks of 52 playing cards represented as an integer array with indexes from 0 to N*52 - 1. You are supplied a random generator that produces numbers from 0 to N - 1 for this task. What is the worst case runtime and memory bound?
-
Random number generator can be used to map the 52 cards of a deck to integers between(1 to 52).
To solve this problem all that has to be done is map each of 52 cards of each deck to correct location and repeat the same card for #deck times.
eg: #decks = 4
so random_num_gen() returns a num between 0 to 51. goto that location and get the card there.Now map that card to correct position in the result array and repeat that card for #deck times.
mapping card to correct location:
to map card number 35 , simple multiply the count of number of elements before it with #decks and add 1 i.e 34*4+1 =137(as 35 has 34 elements before it).so put it in 137 location and repeat the same card for 4 times. -
Time complexity would be O(N).
Space complexity is O(N)
Reply
You must log in to post.