Nearest-Rank Percentile Without Sorting
Problem statement
Given a nonempty integer array values and an integer percentile p from 1 through 100, return the nearest-rank percentile.
Its one-based rank is ceil(p * values.length / 100). Return the value at that rank in sorted order, but do not sort the complete array. You may rearrange values.
Function
nearestRankPercentile(values: int[], p: int) → intExamples
Example 1
values = [7,1,3,9,5]p = 50return = 5The nearest rank is ceil(50 * 5 / 100) = 3; the third sorted value is 5.
Example 2
values = [10,20,30,40]p = 75return = 30The nearest rank is 3, so the answer is 30.
Constraints
1 <= values.length <= 200000-10^9 <= values[i] <= 10^91 <= p <= 100