938. Count of Smaller Numbers After Self (BST/BIT)

HardTreesBITFenwick TreeBST

Given an integer array, return a new array where each element is the count of numbers to its right that are strictly smaller than it. Use a Binary Indexed Tree (or balanced BST) over ranks for an efficient solution. The input is a JSON array.

Input: A JSON array of integers.

Output: Array — counts of smaller elements to the right.

Examples

Example 1
Input: [5,2,6,1]
Output: [2,1,1,0]
Explanation: Counts of smaller elements to the right.
Example 2
Input: [-1,-1]
Output: [0,0]
Explanation: No smaller elements.
Example 3
Input: [0]
Output: [0]
Explanation: Single element.

Constraints

Asked by

GoogleInfosysBloombergMicrosoftAmazonMeta
Solve this problem in the editor →