Ctrl K

How Many Are Smaller

Easy
You are given an integer array values. For each element values[i], count how many elements of the array are strictly smaller than it. Return an array of the same length whose position i holds the count for values[i].
Example 1
values=[4, 9, 4, 0] → [1, 3, 1, 0]

Only 0 is below 4; three values (4, 4 and 0) are below 9; nothing is below 0. Equal values do not count.

Example 2
values=[10, 20, 30] → [0, 1, 2]

The array is already sorted, so each answer equals the index.

Example 3
values=[2, 2] → [0, 0]
Constraints
  • 2 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSort by a Reference Order EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyThe Lost Ticket EasyValues in Both Lists Easy
loading Python…
Input
values=[4, 9, 4, 0]
Expected
[1, 3, 1, 0]

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.