

Find kth Smallest and Largest Element in an Array in C++
source link: https://www.thecrazyprogrammer.com/2021/01/find-kth-smallest-and-largest-element-in-an-array.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find kth Smallest and Largest Element in an Array in C++
Hello everyone, in this post we are going to go through a very popular and recently asked coding question.
Finding the kth smallest and largest element in an array.
From the problem statement, it is clear that the main task is to find the kth smallest or largest element in an unsorted array.
You cannot loop the array and try to find the solution as we do for the minimum or maximum element as in the case of the kth element it is difficult to keep track of the number of elements before any particular element.
Method 1: By Sorting Array
If the array is sorted then it is easy to find the kth smallest or largest element. Fetching arr[k-1] will give us the kth smallest and fetching arr[n-k] will give us the kth largest element, as we just need to find kth element from start and end. We can simply, therefore, sort the array and find the element. Below is the code illustration of the same.
Output:
kth smallest: 2
kth largest: 5
The time complexity for the above approach is O(nlogn) because of sorting cost and space complexity is O(1). If the array order is to be maintained then a copy of the array is required on which sorting can be done, in the case space complexity will be O(n).
Method 2: Using Max and Min Heap
Max-heap: Every element with size k will have a parent greater than both of the child nodes. So a max heap of size k will hold the greatest element at the root node and all the other small elements will be ancestors of that node.
The idea to find kth smallest element:
First, build a max heap with the first k elements, now the heap root node will hold the largest of all k elements.
Iterate through the rest of the elements.
If the current element is greater than the root node then that element need not be included as we already have k small elements so this element won’t add value in our final answer.
If the current element is smaller than the root node then the greatest element i.e. root node element can be removed from the heap as now we have other k small elements than the root node element. So just replace the root node element with the new element and call heapify at the root node so the Heap can be rearranged accordingly.
Here is an illustration of the same with code:
Output:
Kth smallest: 2
The time complexity for the building of Heap is O(k) and for checking for remaining n-k elements its O(logn) per element as maxHeapify cost O(logn) so the overall complexity becomes O(k + (n-k)logn). This method is used widely to find the kth smallest element. Using a Min heap instead of the max heap can similarly be used to find kth largest element as below:
Output:
Kth largest: 4
The time complexity for this remains the same as explained earlier.
Method 3: Quick Sort Variation
While doing a quick sort on an array we select a pivot element and all the elements smaller than that particular element are swapped to the left of the pivot and all the elements greater are swapped to the right of the pivot.
While trying to find kth smallest element, the interesting thing that can be observed is if the partitioning of the array is done based on the pivot, there can arise three conditions
- Index of the pivot is k i.e there are k-1 smaller elements to the left of pivot and others are no right (not necessarily sorted) and so the kth element is the pivot and we return it as an answer.
- Index of the pivot is greater than k i.e. there are more than k smaller elements to the left and therefore we need not sort the right side of the array and need to call the sort function only on the left side.
- Index of the pivot is less than k i.e. there are less than k elements on the left side (say l) and therefore we need not sort the left side and can now find the (k-l)th smallest element on the right side. l elements are subtracted because we already have l elements on the left side of the array.
Below is the implementation of the above logic:
Output:
Kth smallest: 2
The time complexity of the above code in the worst case would be O(n2) and the worst case will occur if the elements are sorted in descending order and k = n.
For ex. 6 5 4 3 2 1 and we have to find the 6th largest element. This is the same as for Quick sort as we always have to query the right side n times.
But average case time complexity for the above algorithm is O(n) as partitioning is a linear operation and there are less number of recursive calls. This method is widely used in practical implementations. By doing some smart selections and some pre-computations on the array the worst case time complexity for the above code can be brought down to O(n).
Recommend
-
12
Leetcode 230. Kth Smallest Element in a BST 当前位置:XINDOO > 算法 > 正文 Given a binary sea...
-
7
-
7
Maximize the smallest array element by incrementing all elements in a K-length subarray by 1 exactly M timesDifficulty Level : Medium...
-
5
Java Program to Find Largest Element in an ArrayJava Program to Find Largest Element in an Array110 Views01/08/2022In this video,...
-
2
本文总结经典的区间第k小值数据结构题。 给定一个长为n的数组,元素为范围为[0,σ)的整数。有m个询问:求区间[l,r)中第k小的元素。 一些方法支持扩展问题:有m个操作,或者修改某个位置上的元素,或者询问区间[l,r)中第k小的元素。
-
10
Find the XOR of smallest and largest triangular number in a given rangeGiven a range, find the XOR of the smallest and largest triangular numbers within that range.
-
10
Find Kth Largest/Smallest element in a QueueGiven a queue of integers and an integer K, our task is to write a program that efficiently finds Kth largest/sma...
-
10
Given a sorted array, find the smallest positive integer that is not the sum of a subset of the array. Youtube Videos Videos Channels R...
-
6
JavaScript Program to Find Largest Element in an ArraySkip to content
-
5
Javascript Program to Find the Largest Element in an ArraySkip to content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK