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
Visualization
Time & Space Complexity
n for frequency count + k² for building smaller count map where k is unique numbers
Space for frequency map and smaller count map where k is number of unique elements
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?