| Difficulty | Medium | |||
|---|---|---|---|---|
| Source | 160 Days of Problem Solving | |||
| Tags |
|
The problem can be found at the following link: Question Link
Given an array arr[] of positive integers and an integer k, your task is to return the k largest elements in decreasing order.
arr = [12, 5, 787, 1, 23]
k = 2
[787, 23]
- The 1st largest element is 787.
- The 2nd largest element is 23.
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
[50, 30, 23]
- The 3 largest elements in descending order are 50, 30, and 23.
arr = [12, 23]
k = 1
[23]
- The 1st largest element is 23.
$(1 \leq k \leq arr.size() \leq 10^6)$ $(1 \leq arr[i] \leq 10^6)$
- Use
nth_element()to partition theklargest elements inO(N). - Use
partial_sort()to sort the topkelements inO(K log K). - Extract and return the
klargest elements in descending order.
- Use
nth_element()to place the k-th largest element atarr.end() - k. - Use
partial_sort()to sort only the k largest elements in descending order. - Return the last
kelements inarr.
- Expected Time Complexity:
O(N + K log K), due to partitioning and sorting. - Expected Auxiliary Space Complexity:
O(1), since sorting is done in-place.
class Solution {
public:
vector<int> kLargest(vector<int>& arr, int k) {
nth_element(arr.begin(), arr.end() - k, arr.end());
partial_sort(arr.end() - k, arr.end(), arr.end(), greater<int>());
return vector<int>(arr.end() - k, arr.end());
}
};- Maintain a min-heap of size
kusing a priority queue. - Push elements into the heap and ensure it only keeps
klargest elements. - Extract elements in descending order from the heap.
class Solution {
public:
vector<int> kLargest(vector<int>& arr, int k) {
priority_queue<int, vector<int>, greater<int>> pq(arr.begin(), arr.begin() + k);
for (int i = k; i < arr.size(); i++)
if (arr[i] > pq.top()) pq.pop(), pq.push(arr[i]);
vector<int> res(k);
while (!pq.empty()) res[--k] = pq.top(), pq.pop();
return res;
}
};🔹 Pros: Efficient for real-time data processing.
🔹 Cons: Extra space (O(K)) for the heap.
- Sort the array in descending order using
sort(). - Return the first
kelements from the sorted array.
class Solution {
public:
vector<int> kLargest(vector<int>& arr, int k) {
sort(arr.rbegin(), arr.rend());
return vector<int>(arr.begin(), arr.begin() + k);
}
};🔹 Pros: Simple to implement.
🔹 Cons: Inefficient for large N due to sorting.
| Approach | ⏱️ Time Complexity | 🗂️ Space Complexity | ⚡ Method | ✅ Pros | |
|---|---|---|---|---|---|
| Optimized Partial Sort | 🟢 O(N + K log K) |
🟢 O(1) |
Partial Sort | Best runtime & space efficiency | None |
| Min-Heap (Priority Queue) | 🟡 O(N log K) |
🟡 O(K) |
Heap-based | Good for streaming data | Extra space usage |
| Sorting Approach | 🔴 O(N log N) |
🟢 O(1) |
Sorting | Simple & easy to implement | Slow for large N |
- ✅ For best efficiency: Partial Sort (
O(N + K log K),O(1)). - ✅ For real-time data processing: Min-Heap (
O(N log K),O(K)). - ✅ For simplicity: Sorting Approach (
O(N log N),O(1)).
class Solution {
public ArrayList<Integer> kLargest(int[] arr, int k) {
Arrays.sort(arr);
ArrayList<Integer> res = new ArrayList<>();
for (int i = arr.length - 1; i >= arr.length - k; i--) res.add(arr[i]);
return res;
}
}class Solution:
def kLargest(self, arr, k):
return sorted(arr, reverse=True)[:k]For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐