Range Queries Fenwick tree
608. Count of Smaller Numbers After Self
You are given an array of integers nums. For each position i, count how many elements to the right of
i are strictly smaller than nums[i]. Return the counts as an array counts of the same length, where
counts[i] is the count for nums[i].
Every later element counts, not only the next one. An element equal to nums[i] is not smaller, so it is
not counted.
Example 1
Input: nums = [4, 7, 2, 7, 1, 3]
[3,3,1,2,0,0]Explanation: After the 4 come 2, 1 and 3, all smaller, so counts[0] = 3. After the first 7 come 2, 7, 1 and 3. The
second 7 is equal, so only 2, 1 and 3 count. The last two elements have nothing smaller after them.
Example 2
Input: nums = [-2, 0, -2, -5, 0]
[1,2,1,0,0]Explanation: Only −5 is smaller than the first −2 and comes after it. The first 0 has −2 and −5 after it. The last 0 has nothing after it at all.
Example 3
Input: nums = [-1000000000, 1000000000, 0]
[0,1,0]Explanation: Nothing is smaller than −109. The 109 has the 0 after it.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 105-109 ≤ nums[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.