How Many Numbers Are Smaller Than the Current Number (LeetCode 1365): Solution and Explanation

Easy
Array
Hash Table
Sorting
Counting Sort

For every element of nums, count how many other elements are strictly smaller, and return those counts in the original order. Values are between 0 and 100.

Intuition

The direct answer compares every pair: for each element, scan the whole array and count smaller values. With at most 500 elements that is 250,000 comparisons, which passes, but it ignores the most useful constraint.

Values only range from 0 to 100. That means you can describe the whole array by how often each value appears, using 101 buckets, no matter how long the array is.

Once you have the bucket counts, the number of elements smaller than v is the total of buckets 0 to v - 1. Sweeping the buckets left to right with a running total gives this for every value in one pass.

Storing each count one slot to the right (count[x + 1]) before the sweep makes the prefix sum land exactly where you want it: afterwards count[x] is the number of elements below x, and each answer is a single lookup.

Approach: Counting buckets with a prefix sum

  1. Create count with 102 zeros.
  2. For each x in nums, increment count[x + 1].
  3. For v from 1 to 101, add count[v - 1] into count[v]. Now count[v] is how many elements are smaller than v.
  4. For each x in nums, append count[x] to the result, and return it.

Step-by-step example

nums = [8,1,2,2,3]
  1. Tally: value 1 appears once, 2 twice, 3 once, 8 once.
  2. Sweep: below 1 there are 0 numbers. Add one 1, so below 2 there is 1.
  3. Add two 2s, so below 3 there are 3. Add one 3, so below 8 there are 4.
  4. Look up each element: 8 gives 4, 1 gives 0, 2 gives 1, 2 gives 1, 3 gives 3.
  5. Return [4,0,1,1,3].

Code: Python, Java, C++

1class Solution:
2    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
3        count = [0] * 102
4        for x in nums:
5            count[x + 1] += 1
6        for v in range(1, 102):
7            count[v] += count[v - 1]
8        res = []
9        for x in nums:
10            res.append(count[x])
11        return res
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting buckets with a prefix sum O(n + k)
Sort with indices O(n log n)
Compare every pair O(n²)
Time

O(n + k)

Two passes over the n elements plus one sweep over k = 101 buckets.

Space

O(k)

The bucket array has a fixed 102 slots, independent of n. The output array is not counted.

Other approaches

Compare every pair (time O(n²), space O(1))

For each element, loop over the array and count strictly smaller values. Simple and fast enough for n = 500.

Sort with indices (time O(n log n), space O(n))

Sort a copy of the array. The first index where a value appears in the sorted copy is how many values are smaller than it. Works for any value range.

Common mistakes

  • Counting equal values as smaller. Duplicates must not count, so [7,7,7,7] gives all zeros.
  • Taking the prefix sum without the one-slot shift. Then count[x] includes the copies of x itself, and you would need count[x - 1] with a special case for 0.
  • Sizing the bucket array as 101 when using x + 1. The value 100 writes to index 101, so you need 102 slots.
  • Returning the answers in sorted order instead of the original order of nums.

Frequently asked questions

Why does the shifted prefix sum give strictly smaller counts?

Each value x is tallied at index x + 1. After summing, index v holds the tallies of every value from 0 to v - 1, which are exactly the values strictly smaller than v.

What if values were not bounded by 100?

Then sort a copy of the array and use the first position of each value as its count, which costs O(n log n). Counting only pays off when the value range is small.

Is this the same idea as counting sort?

Yes. Counting sort uses the same prefix sums to decide where each value starts in the sorted output. Here that starting position is the answer itself.