How Many Numbers Are Smaller Than the Current Number

Easy

Topics
ArrayHash TableCounting

For each nums[i], count how many nums[j] (j != i) are strictly smaller. Return the counts as an array.

Example 1

nums81223
answer40113
Each answer cell counts the values strictly smaller than the one above it, found anywhere in the array rather than only to its left. Equal values do not count, so two copies of the same number always get the same answer.
Top row is the input; bottom row the count of strictly smaller values per index.
Input:  nums = [8,1,2,2,3]
Output: [4,0,1,1,3]

Example 2

nums6548
answer2103
Input:  nums = [6,5,4,8]
Output: [2,1,0,3]

Constraints

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

For each element, count how many others are strictly smaller. The direct double loop does exactly that and is O(n^2) — and with n <= 100 it passes, which is why it is tempting to stop there.

It is worth doing better, because the values are also bounded: 1 <= nums[i] <= 100. When the value range is a constant, counting beats comparing. Tally how many times each value occurs, then take a running prefix sum so that less[v] is the number of elements strictly below v. Answering each element is then a single lookup.

💡 This is counting sort's core idea without the sort: the prefix sum over value counts is the rank of each value, and ranks are exactly what the question asks for.

Approach

  1. count[v] for each value in the input.
  2. Prefix sum into less[v] = total occurrences of values < v.
  3. Map each input element through less.

Dry Run

nums = [8,1,2,2,3]

count: 1->1, 2->2, 3->1, 8->1

prefix sums (running total of strictly smaller):
  less[1] = 0
  less[2] = 1          (just the single 1)
  less[3] = 3          (1, 2, 2)
  less[8] = 4          (1, 2, 2, 3)

map the input:
  8 -> 4,  1 -> 0,  2 -> 1,  2 -> 1,  3 -> 3

answer [4,0,1,1,3]

Both 2s map to the same answer, which is the point of counting strictly smaller values — ties do not count each other.

Complexity

  • Time — O(n + k): where k is the size of the value range, 101 here, so O(n). The double loop is O(n^2); at the constraint limit that is 10,000 comparisons against about 300 operations, and the gap grows if the length bound ever rises.
  • Space — O(k) = O(1): two fixed 101-entry arrays, independent of input size. The output is the result rather than scratch space.

Common Pitfalls

  • Claiming O(n) time for the nested-loop version. It is O(n^2); the counting version is what makes the linear claim true.
  • Taking the prefix sum inclusively, so less[v] counts values <= v. That over-counts by the number of ties.
  • Sizing the arrays to max(nums) without the +1, or to n instead of the value range. The arrays are indexed by value, not by position.
  • Sorting and using the first index of each value. It works and is O(n log n), but needs care with duplicates and is strictly worse here.

Full Solution — all languages

class Solution:
    def smallerNumbersThanCurrent(self, nums):
        # Values are bounded 1..100, so count occurrences instead of comparing pairs.
        count = [0] * 101
        for x in nums:
            count[x] += 1
        # less[v] = how many values are strictly below v, by running prefix sum.
        less = [0] * 101
        running = 0
        for v in range(101):
            less[v] = running
            running += count[v]
        return [less[x] for x in nums]
SensAI
Beta
Tap the mic to stop voice mode