Skip to content
Advertisement

How to find the smallest threshold from one list to fit another list

I have two lists of marks for the same set of students. For example:

JavaScript

If I accept only students above a threshold according to list A then I can look up their marks in list B. For example, if I only accept students with at least a mark of 80 from list A then their marks in list B are [6, 99, 45].

I would like to compute the smallest threshold for A which gives at least 90% of students in the derived set in B getting at least 50. In this example the threshold will have to be 93 which gives the list [99] for B.

Another example:

JavaScript

In this case we have to set the threshold to 66 which then gives 100% of [60, 80, 80, 60] getting at least 50.

Advertisement

Answer

This is an O(nlogn + m) approach (due to sorting) where n is the length of A and m is the length of B:

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement