How Many Numbers Are Smaller Than the Current Number
Easy
Topics
For each nums[i], count how many nums[j] (j != i) are strictly smaller. Return the counts as an array.
Example 1
Input: nums = [8,1,2,2,3] Output: [4,0,1,1,3]
Example 2
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
count[v]for each value in the input.- Prefix sum into
less[v]= total occurrences of values< v. - 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
kis 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 toninstead 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]import java.util.*;
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
// Values are bounded 1..100, so count occurrences instead of comparing pairs.
int[] count = new int[101];
for (int x : nums) count[x]++;
// less[v] = how many values are strictly below v, by running prefix sum.
int[] less = new int[101];
int running = 0;
for (int v = 0; v <= 100; v++) {
less[v] = running;
running += count[v];
}
int[] out = new int[nums.length];
for (int i = 0; i < nums.length; i++) out[i] = less[nums[i]];
return out;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
// Values are bounded 1..100, so count occurrences instead of comparing pairs.
vector<int> count(101, 0);
for (int x : nums) count[x]++;
// less[v] = how many values are strictly below v, by running prefix sum.
vector<int> less(101, 0);
int running = 0;
for (int v = 0; v <= 100; v++) {
less[v] = running;
running += count[v];
}
vector<int> out;
out.reserve(nums.size());
for (int x : nums) out.push_back(less[x]);
return out;
}
};/**
* @param {number[]} nums
* @return {number[]}
*/
var smallerNumbersThanCurrent = function (nums) {
// Values are bounded 1..100, so count occurrences instead of comparing pairs.
const count = new Array(101).fill(0);
for (const x of nums) count[x]++;
// less[v] = how many values are strictly below v, by running prefix sum.
const less = new Array(101).fill(0);
let running = 0;
for (let v = 0; v <= 100; v++) {
less[v] = running;
running += count[v];
}
return nums.map((x) => less[x]);
};