Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
The intended solution uses Fenwick Tree.
Let’s describe the solution for counting the number of elements smaller than nums[i] in the range [0, i - 1], and counting in the range [i + 1, nums.length - 1] can be done similarly by simply reversing the array.
Iterate from left to right and maintain the Fenwick Tree. Save the value of the query(nums[i]) and update nums[i] + 1 by 1.