How Many Numbers Are Smaller Than the Current Number - Problem

Given an array nums, your task is to find out how many numbers are smaller than each element in the array.

For each element nums[i], count how many elements in the array have values strictly less than nums[i]. Note that we don't count the element itself - only other elements that are smaller.

Goal: Return an array where each position contains the count of smaller numbers for the corresponding element in the input array.

Example: If nums = [8,1,2,2,3], then:
• For 8: numbers 1,2,2,3 are smaller → count = 4
• For 1: no numbers are smaller → count = 0
• For 2: number 1 is smaller → count = 1
• For 2: number 1 is smaller → count = 1
• For 3: numbers 1,2,2 are smaller → count = 3
Result: [4,0,1,1,3]

Input & Output

example_1.py — Basic case
$ Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
💡 Note: For 8: elements 1,2,2,3 are smaller (count=4). For first 1: no elements are smaller (count=0). For first 2: element 1 is smaller (count=1). For second 2: element 1 is smaller (count=1). For 3: elements 1,2,2 are smaller (count=3).
example_2.py — All same elements
$ Input: nums = [6,5,4,8]
Output: [2,1,0,3]
💡 Note: For 6: elements 5,4 are smaller (count=2). For 5: element 4 is smaller (count=1). For 4: no elements are smaller (count=0). For 8: elements 6,5,4 are smaller (count=3).
example_3.py — Single element
$ Input: nums = [7,7,7,7]
Output: [0,0,0,0]
💡 Note: All elements are equal, so no element is smaller than any other element. Each element has 0 elements smaller than it.

Visualization

Tap to expand
🎓 Student Grade Ranking ProcessStep 1: Students with their scores8Alice1Bob2Carol2Dave3EveStep 2: Frequency CountScore 11 studentScore 22 studentsScore 31 studentScore 81 studentStep 3: Count Students with Lower ScoresScore 10 lower(none below 1)Score 21 lower(1 student < 2)Score 33 lower(1+2 students < 3)Score 84 lower(1+2+1 students < 8)Final Result: [4,0,1,1,3]4Alice0Bob1Carol1Dave3Eve🎯 Key Insight: Instead of each student asking everyone individually, use a frequency chart for efficient lookups!
Understanding the Visualization
1
Collect All Scores
Each student reports their score: [8,1,2,2,3]
2
Count Frequency
Tally how many students got each score: 1→1 student, 2→2 students, 3→1 student, 8→1 student
3
Calculate Rankings
For each score, sum up students who scored lower: 1→0 lower, 2→1 lower, 3→3 lower, 8→4 lower
4
Assign Results
Each student gets their ranking: [4,0,1,1,3]
Key Takeaway
🎯 Key Insight: By using frequency counting, we transform an O(n²) problem into O(n + k²) or even O(n + k) with counting sort, dramatically improving efficiency for arrays with many duplicate values.

Time & Space Complexity

Time Complexity
⏱️
O(n + k²)

n for frequency count + k² for building smaller count map where k is unique numbers

n
2n
Linear Growth
Space Complexity
O(k)

Space for frequency map and smaller count map where k is number of unique elements

n
2n
Linear Space

Constraints

  • 2 ≤ nums.length ≤ 500
  • 0 ≤ nums[i] ≤ 100
  • Follow-up: Can you solve it in O(n + k) time where k is the range of input values?
Asked in
Amazon 25 Google 18 Microsoft 15 Apple 12
95.0K Views
Medium Frequency
~15 min Avg. Time
3.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen