Count of Smaller Numbers After Self

Hard

Description

Given an integer array nums, return an array counts where counts[i] is the number of smaller elements to the right of nums[i].

Examples

Input:nums = [5,2,6,1]
Output:[2,1,1,0]
Explanation:

For 5, there are 2 smaller elements (2, 1) to its right.

Input:nums = [-1]
Output:[0]
Explanation:

Works with negative numbers.

Input:nums = [3,4,3,2,1]
Output:[1,3,2,1,0]
Explanation:

For index 0 (value 3): only 1 smaller element (1) to the right. For index 1 (value 4): 3 smaller elements (3,2,1) to the right. For index 2 (value 3): 2 smaller elements (2,1) to the right. For index 3 (value 2): 1 smaller element (1) to the right. For index 4 (value 1): 0 smaller elements to the right. This example demonstrates handling duplicate values and a descending sequence at the end.

Constraints

  • 1 ≤ nums.length ≤ 10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!