How Many Numbers Are Smaller Than the Current Number

Given an array `nums`, for every `nums[i]` count how many other entries `nums[j]` are strictly smaller than it, and return those counts as an array in the same order.

30:00

How Many Numbers Are Smaller Than the Current Number
easy
Free written solution
Topics
Companies

Given an array nums, for every nums[i] count how many other entries nums[j] are strictly smaller than it, and return those counts as an array in the same order.

Example 1:
Input: {"nums":[8,1,2,2,3]}
Output: [4,0,1,1,3]
Constraints:
  • 2≤nums.length≤5002 \leq \text{nums.length} \leq 500

  • 0≤nums[i]≤1000 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[8,1,2,2,3]}

nums and res

8
?
1
?
2
?
2
?
3
?

buckets: count per value

1
-
2
-
3
-
8
-

Purple: how many numbers are strictly smaller than the bucket's value (the prefix sum of the buckets to its left).

Values only go from 0 to 100, so instead of comparing every pair we count how many times each value appears.

Variables
VariableValue
n5
DepthFunction Call
Stack empty
0/17