304 North Cardinal St.
Dorchester Center, MA 02124

# Kth Largest Element in an Array LeetCode Solution

## Problem – Kth Largest Element in an Array

Given an integer array `nums` and an integer `k`, return the `kth` largest element in the array.

Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.

You must solve it in `O(n)` time complexity.

Example 1:

``````Input: nums = [3,2,1,5,6,4], k = 2
Output: 5``````

Example 2:

``````Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
``````

Constraints:

• `1 <= k <= nums.length <= 105`
• `-104 <= nums[i] <= 104`

### Kth Largest Element in an Array LeetCode Solution in C++

``````class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
buildMaxHeap(nums);
for (int i = 0; i < k - 1; i++) {
swap(nums, nums[--heapSize]);
maxHeapify(nums, 0);
}
return nums;
}
private:
int heapSize;

int left(int i) {
return (i << 1) + 1;
}

int right(int i) {
return (i << 1) + 2;
}

void maxHeapify(vector<int>& nums, int i) {
int largest = i, l = left(i), r = right(i);
if (l < heapSize && nums[l] > nums[largest]) {
largest = l;
}
if (r < heapSize && nums[r] > nums[largest]) {
largest = r;
}
if (largest != i) {
swap(nums[i], nums[largest]);
maxHeapify(nums, largest);
}
}

void buildMaxHeap(vector<int>& nums) {
heapSize = nums.size();
for (int i = (heapSize >> 1) - 1; i >= 0; i--) {
maxHeapify(nums, i);
}
}
};
``````

### Kth Largest Element in an Array LeetCode Solution in Python

``````class Solution:
def findKthLargest(self, nums, k):
if not nums: return
pivot = random.choice(nums)
left =  [x for x in nums if x > pivot]
mid  =  [x for x in nums if x == pivot]
right = [x for x in nums if x < pivot]

L, M = len(left), len(mid)

if k <= L:
return self.findKthLargest(left, k)
elif k > L + M:
return self.findKthLargest(right, k - L - M)
else:
return mid
``````

### Kth Largest Element in an Array LeetCode Solution in Java

``````public int findKthLargest(int[] nums, int k) {
return quickSelect(nums, 0, nums.length - 1, k);
}

int quickSelect(int[] nums, int low, int high, int k) {
int pivot = low;

// use quick sort's idea
// put nums that are <= pivot to the left
// put nums that are  > pivot to the right
for (int j = low; j < high; j++) {
if (nums[j] <= nums[high]) {
swap(nums, pivot++, j);
}
}
swap(nums, pivot, high);

// count the nums that are > pivot from high
int count = high - pivot + 1;
// pivot is the one!
if (count == k) return nums[pivot];
// pivot is too small, so it must be on the right
if (count > k) return quickSelect(nums, pivot + 1, high, k);
// pivot is too big, so it must be on the left
return quickSelect(nums, low, pivot - 1, k - count);
}
``````
##### Kth Largest Element in an Array LeetCode Solution Review:

In our experience, we suggest you solve this Kth Largest Element in an Array LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Kth Largest Element in an Array LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Kth Largest Element in an Array LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions