# Kth Largest element in an array

This 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 C

Sample Output:

```The 4th largest element = 12
```

### In Python

Sample Output:

```The 5th largest element = 8
```

### C++ code

Sample 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-Heap

The 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++ Code

Output:

```The 2th largest element = 16
```

### C Code

Output:

```The 3th largest element = 12
```

## Approach 4 - Using Min Heap

In 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:

• If the element would be smaller than the root means the kth largest value is still the root element and we check the condition for the next element.
• If the element would be greater than the root means the root element is not the kth largest value and we replace the root with the element and call min-heapify at the root index.
• At the end of the execution, we will return the root element, which would be the kth largest value.

The time complexity of this approach is T(n) = klogk + (n-k)logk

And the space complexity = O(k)

### C++ code

Output:

```My array = {12, 15, 17, 23, 9, 16, 7, 45, 6, 42, 33}
The 5th largest value = 17
```

### C code

Output:

```My array = {12, 15, 17, 23, 9, 16, 7, 45, 6, 42, 33, }
The 4th largest value = 23
```

## Approach 5 - Partial Quick Sort

In 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 Code

Output:

```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
```