Count Smaller Numbers After Self — Merge Sort or Binary Indexed Tree
Count smaller elements to the right of each element using merge sort with position tracking in O(n log n).
webcoderspeed.com
6 articles
Count smaller elements to the right of each element using merge sort with position tracking in O(n log n).
Support point updates and range sum queries. BIT (Fenwick Tree) gives O(log n) both operations with elegant implementation.
Count smaller elements to the right of each element. Process from right to left; BIT tracks seen elements. Coordinate compress values to BIT indices.
Count pairs (i,j) where i<j and nums[i] > 2*nums[j]. Modified merge sort: during merge, count pairs across left/right halves.
Count subarray sums within [lower, upper]. Use prefix sums and merge sort: during merge count pairs of prefix sums with difference in range.
Find max rectangle sum <= k in 2D matrix. Fix row boundaries, compress to 1D array, use prefix sums + BIT/sorted set to find best subarray sum <= k.