Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What algorithm can be used here [closed]

This is an interview question, not a homework.

N friends are playing a game. Each of them has a list of numbers in front of himself.

Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.

I must the count all possible numbers that the game administrator can shout.

For example, if N = 3 and K = 3, and the lists for the 3 friends are 2 5 3, 8 1 6, 7 4 9. The output is 6, since the possible values are 4 5 6 7 8 9.

Can anybody suggest a decent algorithm for this problem? What I am doing is to create all possible permutations taking one number from each list, then sorting the resultant and printing the kth-largest. But that takes too much time.

like image 679
SexyBeast Avatar asked Nov 30 '25 02:11

SexyBeast


2 Answers

To know if a number is present in the result or not, you need to know for each other list if there are numbers above and if there are numbers below. List where there are both numbers above and below are not a problem as you can choose a number in them as it suits you. The problem are lists where there are only numbers above or only numbers below. The first ones must be at most N-K, the second ones must be at most K. If this is not true, your number cannot be picked. If this is true, you can always choose numbers in the lists where there are both number above and below so that your number is picked.

This can be checked in linear time, or even better if your first sort your lists, thus giving an overall complexity of O(n.log(n)) where n is the number of numbers in total. Without sorting you got a n² complexity.

In your example with lists :

{2 5 3}, {8 1 6}, {7 4 9}

say we are looking for the 2-greatest number. For each number we ask if it can be shout by the administator. For each of them we look if in the other list there are both numbers below and numbers above. Let's look further for some of them

  • For 5 : there is numbers above and below in both other lists. So "5" can be shout by the administrator.

  • For "2" : there is numbers above and below in the second list so I can freely choose a number above or below in this list. But there are only numbers above in the third list, so the picked number in this list will always be greater. Since i can freely choose a number below in the second list, thus making my "2" the 2nd greatest number.

  • For "1" : there is only numbers above in the second list, so "1" will always be the smallest element.

  • For "9" : this is the other way round, it is always the greatest.

like image 107
Valentin Perrelle Avatar answered Dec 03 '25 02:12

Valentin Perrelle


take the smallest number from each set. find the K-th -largest of these. This is the smallest number that is in the result. Similarly, find the largest number in the result. Prove that Each number between the two is in the result.

like image 31
maniek Avatar answered Dec 03 '25 03:12

maniek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!