How Many Numbers Are Smaller Than the Current Number

Very easy Sorting

nums is a list of whole numbers from 0 to 10⁴.

For each number of nums, count the numbers in nums that are smaller than it. A number equal to it does not count, and nor does the number itself.

Return the counts as a list in the order of nums: the count for nums[i] at position i.

Example 1

Input: nums = [5,0,9,5,2]
Output: [2,0,4,2,1]

Two numbers are smaller than 5, the 0 and the 2. The other 5 is equal to it, so it does not count, and both 5s get 2. The 9 is larger than the four other numbers, so it gets 4.

Example 2

Input: nums = [3,1,4,1,6]
Output: [2,0,3,0,4]

Example 3

Input: nums = [12,11,10,9]
Output: [3,2,1,0]

Constraints

  • 1 ≤ nums.length ≤ 2 × 10⁵
  • 0 ≤ nums[i] ≤ 10⁴

Practice this in an AI round

30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.

See the AI round

Watch the explanation

More Sorting

All Sorting problems