Percentile

EasyStatisticsMathArraySorting

Description

Given an array of numbers and a percentile p between 0 and 100, return the p-th percentile using linear interpolation: sort the data, compute the rank index p/100 * (n - 1), and interpolate linearly between the two nearest sorted values. Round the result to 4 decimal places.

Examples

Input:[1,2,3,4,5], 50
Output:3
Explanation:

The rank index lands between two sorted values, and linear interpolation between those neighbours gives the requested percentile.

Input:[1,2,3,4], 25
Output:1.75
Explanation:

The rank index lands between two sorted values, and linear interpolation between those neighbours gives the requested percentile.

Input:[10,20,30,40], 90
Output:37
Explanation:

The rank index lands between two sorted values, and linear interpolation between those neighbours gives the requested percentile.

Constraints

  • 1 ≤ array length ≤ 10⁴
  • 0 ≤ p ≤ 100

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.