Count of Smaller Numbers After Self - Problem
Count of Smaller Numbers After Self
Imagine you're standing in a line with friends, and you want to know how many people behind you are shorter than you. This classic problem asks: given an integer array
For example, if
• Position 0 (value 5): Elements [2,6,1] are to the right, and 2 of them (2,1) are smaller than 5
• Position 1 (value 2): Elements [6,1] are to the right, and 1 of them (1) is smaller than 2
• Position 2 (value 6): Elements [1] are to the right, and 1 of them (1) is smaller than 6
• Position 3 (value 1): No elements to the right, so 0
Result:
This problem appears frequently in coding interviews and has multiple elegant solutions ranging from brute force to advanced data structures.
Imagine you're standing in a line with friends, and you want to know how many people behind you are shorter than you. This classic problem asks: given an integer array
nums, return an array counts where counts[i] represents the number of elements to the right of position i that are smaller than nums[i].For example, if
nums = [5,2,6,1]:• Position 0 (value 5): Elements [2,6,1] are to the right, and 2 of them (2,1) are smaller than 5
• Position 1 (value 2): Elements [6,1] are to the right, and 1 of them (1) is smaller than 2
• Position 2 (value 6): Elements [1] are to the right, and 1 of them (1) is smaller than 6
• Position 3 (value 1): No elements to the right, so 0
Result:
[2,1,1,0]This problem appears frequently in coding interviews and has multiple elegant solutions ranging from brute force to advanced data structures.
Input & Output
example_1.py — Basic Case
$
Input:
nums = [5,2,6,1]
›
Output:
[2,1,1,0]
💡 Note:
To the right of 5 there are 2 smaller elements (2 and 1). To the right of 2 there is 1 smaller element (1). To the right of 6 there is 1 smaller element (1). To the right of 1 there are 0 smaller elements.
example_2.py — All Decreasing
$
Input:
nums = [6,5,4,3,2,1]
›
Output:
[5,4,3,2,1,0]
💡 Note:
Since the array is decreasing, each element has all remaining elements to its right as smaller elements. Element 6 has 5 smaller elements to its right, element 5 has 4 smaller elements, and so on.
example_3.py — Single Element
$
Input:
nums = [1]
›
Output:
[0]
💡 Note:
There are no elements to the right of the single element, so the count is 0.
Visualization
Tap to expand
Understanding the Visualization
1
Start from the rightmost
Begin with the last element - it has 0 elements to its right
2
Build sorted collection
As we move left, maintain a sorted list of elements we've seen
3
Binary search for position
For each new element, binary search tells us how many seen elements are smaller
4
Insert and continue
Add current element to sorted list and move to next position
Key Takeaway
🎯 Key Insight: Process from right-to-left while maintaining sorted order enables efficient counting through binary search, transforming an O(n²) problem into a more manageable solution.
Time & Space Complexity
Time Complexity
O(n²)
For each of n elements, we scan up to n-1 remaining elements
⚠ Quadratic Growth
Space Complexity
O(1)
Only constant extra space besides the result array
✓ Linear Space
Constraints
- 1 ≤ nums.length ≤ 105
- -104 ≤ nums[i] ≤ 104
- Array elements can be negative, positive, or zero
- Duplicate elements are allowed
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code