dsa3 min read
Find K Closest Elements
Find K closest elements to x in a sorted array using binary search to find the optimal left boundary.
Read →
1575 articles
Find K closest elements to x in a sorted array using binary search to find the optimal left boundary.
Find the Kth largest element using quickselect O(n) average or a min-heap of size K in O(n log k).
Return the K most frequent elements using a frequency map plus min-heap, or bucket sort for O(n) solution.
Sort characters in a string by decreasing frequency using a frequency counter and max-heap or bucket sort.
Find K points closest to the origin using a max-heap of size K or quickselect for O(n) average.