> For the complete documentation index, see [llms.txt](https://dongzeli95s-organization.gitbook.io/swe-interview-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dongzeli95s-organization.gitbook.io/swe-interview-handbook/algorithm/heap/kth_largest_element.md).

# Kth Largest Element

```cpp
// https://leetcode.com/problems/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.
Can you solve it without sorting?

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

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

*/

#include <vector>
#include <queue>
#include <cassert>

using namespace std;

// Time complexity: O(N*logK), Space complexity: O(K)
int findKthLargest(vector<int>& nums, int k) {
    if (nums.size() < k) {
        return -1;
    }

    priority_queue<int, vector<int>, greater<int>> min_heap;
    
    int n = nums.size();
    for (int i = 0; i < n; i++) {
        min_heap.push(nums[i]);
        if (min_heap.size() > k) {
            min_heap.pop();
        }
    }

    return min_heap.top();
}

int main() {
    vector<int> nums1 = {3,2,1,5,6,4};
    assert(findKthLargest(nums1, 2) == 5);

    vector<int> nums2 = {3,2,3,1,2,4,5,5,6};
    assert(findKthLargest(nums2, 4) == 4);

    return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dongzeli95s-organization.gitbook.io/swe-interview-handbook/algorithm/heap/kth_largest_element.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
