How Many Numbers Are Smaller Than the Current Number
You are given an array nums
, and you need to find out, for each element, how many numbers in the array are smaller than it. Return the answer in an array.
Approach
We’ll use the following approach to solve this problem:
 Create a result list to store the count of smaller numbers for each number in
nums
.  For each number in
nums
, count how many numbers are smaller than it, and store the count in the result list.  Return the result list.
Here’s the code:


Example
Let’s take nums = [8,1,2,2,3]
as an example:
 For
8
, there are4
smaller numbers(1, 2, 2, 3)
.  For
1
, there are0
smaller numbers.  For
2
, there is1
smaller number(1)
.  For the other
2
, there is1
smaller number(1)
.  For
3
, there are3
smaller numbers(1, 2, 2)
.
So the output will be [4,0,1,1,3]
.
This solution works but is not highly efficient. It has a time complexity of (O(n^2)) where (n) is the length of the array nums
. For larger inputs, a more optimized solution can be implemented, but this code is simple and clear for beginners and will work well within the given constraints.