Count of Smaller Numbers After Self [Hard] — Merge Sort
Count elements smaller than each element to its right using a modified merge sort that counts inversions during the merge step.
webcoderspeed.com
12 articles
Count elements smaller than each element to its right using a modified merge sort that counts inversions during the merge step.
Find the median of two sorted arrays in O(log(min(m,n))) by binary searching for the correct partition point.
Count pairs (i,j) where i<j and nums[i]>2*nums[j] using modified merge sort to count cross-half pairs before merging.
Find the kth smallest element from two sorted arrays in O(log(m+n)) using binary elimination of k/2 elements per step.
Sort a linked list in O(n log n) time and O(log n) space using top-down merge sort with fast/slow split.
Convert a sorted linked list to a height-balanced BST by repeatedly finding the middle node as root.
Merge k sorted linked lists into one sorted list using a min-heap for O(n log k) time complexity.
Build a binary tree from preorder and inorder traversals by using the preorder root and inorder index to split left/right subtrees.
Build a quad tree from a 2D grid by recursively checking if a region is uniform and splitting into four quadrants.
Count tuples (i,j,k,l) where nums1[i]+nums2[j]+nums3[k]+nums4[l]=0 by hashing all pairs from first two arrays.
Count the number of inversions in an array (pairs where i < j but arr[i] > arr[j]) using modified merge sort in O(n log n). Google uses this to test deep algorithm understanding.
Find the median of two sorted arrays in O(log(min(m,n))) time using binary search on partition points. A classic hard problem testing deep binary search understanding.