How Many Numbers Are Smaller Than the Current Number

Given the array nums, for each nums[i], find out how many numbers in the array are smaller than it. That is, for each nums[i], count the number of valid j values such that j != i and nums[j] < nums[i].

Return the answer in an array.

Example 1
Inputnums = [8,1,2,2,3]
Output[4,0,1,1,3]
For each element, the counts of smaller numbers are 4 for 8, 0 for 1, 1 for each 2, and 3 for 3.
Example 2
Inputnums = [6,5,4,8]
Output[2,1,0,3]
For 6 there are two smaller numbers, for 5 there is one, for 4 there are none, and for 8 there are three.

Constraints

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate