Kth Largest element in an arrayThis article will teach us to find the kth largest element in an unsorted array. There are different ways to find the solution to the given problem. The best possible practices are discussed below: Problem - Consider an unsorted array with N number of elements. A number k less than the size of the array is given; we have to find the kth largest element in the best possible ways. For example: Approach 1: The most straightforward method that comes to mind is sorting the given array and returning the required value. We can find the solution in O(N Log N) time using either the Heap Sort or Merge Sort. Assuming the array has no identical elements: In C++Sample Output: The 3th largest element = 15 In CSample Output: The 4th largest element = 12 In PythonSample Output: The 5th largest element = 8 C++ codeSample Output: My set = {2, 4, 5, 10, 12, 18, 22, 36, 54} The 3th largest element = 22 Python code:Output: My set = {8, 9, 10, 12, 15, 17, 22, 26, 27, 28, 29} The 5th largest element = 22 Approach 3 - Using Max-HeapThe kth largest element can be found with the help of the heap. Here, we have used max heap; we first convert the given array into a max-heap first, then replace the root with the last element, decrease the heap size by one and then convert it into a max-heap again. We follow the same steps k-1 times. After that, the kth element is found at the root index = 0. C++ CodeOutput: The 2th largest element = 16 C CodeOutput: The 3th largest element = 12 Approach 4 - Using Min HeapIn this method, we create a min heap with k number of elements and the minimum of that (root element) would be the kth largest value. If there would be more than k elements in the array, then we check each element whether it is greater than the root element of the heap or not, we will follow the below steps for all the remaining elements:
The time complexity of this approach is T(n) = klogk + (n-k)logk And the space complexity = O(k) C++ codeOutput: My array = {12, 15, 17, 23, 9, 16, 7, 45, 6, 42, 33} The 5th largest value = 17 C codeOutput: My array = {12, 15, 17, 23, 9, 16, 7, 45, 6, 42, 33, } The 4th largest value = 23 Approach 5 - Partial Quick SortIn this approach, we use the idea of the quick sort to find the kth largest element in the array. As we know, the quick sort selects a pivot element, places it at its right index, and recursively repeats the same process until the array gets sorted. Similarly, we perform the same steps but not the complete execution of the quick sort. When the pivot is itself the kth largest value, we will stop the execution and print the required value. The worst time complexity of this approach is O(n^2) and the average time complexity is O(N logN). We achieve our desired out in less time than the average case complexity. C++ Code:Output: The array = [45, 66, 85, 69, 33, 25, 18, 12, 14, 13] The 5th largest element = 33 C CodeOutput: The array = [75, 85, 69, 60, 37, 28, 35, 26, 11, 18] The 4th largest element = 60 Output: My array = [78, 41, 33, 32, 25, 20, 15, 14, 12, 11] The 6th largest element = 20 |