Skip to content
Onsite

Count of Smaller Numbers After Self

MediumP3Coding

For every element of nums, count how many elements to its right are strictly smaller. Return the counts as an array counts, where counts[i] is the number of j > i with nums[j] < nums[i].

Example 1

Input
nums = [5,2,6,1]
Output
[2,1,1,0]
Why
5 has two smaller elements to its right (2 and 1), 2 has one (1), 6 has one (1), 1 has none.

Example 2

Input
nums = [-1]
Output
[0]

Example 3

Input
nums = [-1,-1]
Output
[0,0]

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Hints

Run checks the 3 visible tests, Submit all 10.