21. How Many Numbers Are Smaller Than Current

EasyArrayArray

Given the array nums, for each nums[i] count how many numbers in the array are strictly smaller than nums[i]. Return the answer as an array.

Input: An integer array nums of length n.

Output: Integer array where answer[i] = count of numbers smaller than nums[i].

Examples

Example 1
Input: [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation: 8: 4 numbers smaller (1,2,2,3). 1: 0 smaller. 2: 1 smaller (1). 2: 1 smaller. 3: 3 smaller (1,2,2).
Example 2
Input: [6,5,4,8]
Output: [2,1,0,3]
Explanation: 6: [4,5]→2. 5: [4]→1. 4: []→0. 8: [4,5,6]→3.
Example 3
Input: [7,7,7,7]
Output: [0,0,0,0]
Explanation: All equal — no element is strictly less. All answers are 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →