17. K Largest Elements

The problem can be found at the following link: Question Link

Problem Description

Given an array arr[] of positive integers and an integer k, your task is to return the k largest elements in decreasing order.

Examples

Example 1:

Input:

arr = [12, 5, 787, 1, 23]
k = 2

Output:

[787, 23]

Explanation:

  • The 1st largest element is 787.

  • The 2nd largest element is 23.

Example 2:

Input:

Output:

Explanation:

  • The 3 largest elements in descending order are 50, 30, and 23.

Example 3:

Input:

Output:

Explanation:

  • The 1st largest element is 23.

Constraints:

  • $(1 \leq k \leq arr.size() \leq 10^6)$

  • $(1 \leq arr[i] \leq 10^6)$

My Approach

Optimized Partial Sort (O(N + K log K) Time, O(1) Space)

  1. Use nth_element() to partition the k largest elements in O(N).

  2. Use partial_sort() to sort the top k elements in O(K log K).

  3. Extract and return the k largest elements in descending order.

Algorithm Steps:

  1. Use nth_element() to place the k-th largest element at arr.end() - k.

  2. Use partial_sort() to sort only the k largest elements in descending order.

  3. Return the last k elements in arr.

Time and Auxiliary Space Complexity

  • 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.

Code (C++)

⚑ Alternative Approaches

2️⃣ Min-Heap Approach (O(N log K) Time, O(K) Space)

Approach:

  1. Maintain a min-heap of size k using a priority queue.

  2. Push elements into the heap and ensure it only keeps k largest elements.

  3. Extract elements in descending order from the heap.

Code (C++)

πŸ”Ή Pros: Efficient for real-time data processing. πŸ”Ή Cons: Extra space (O(K)) for the heap.

3️⃣ Sorting Approach (O(N log N) Time, O(1) Space)

Approach:

  1. Sort the array in descending order using sort().

  2. Return the first k elements from the sorted array.

Code (C++)

πŸ”Ή Pros: Simple to implement. πŸ”Ή Cons: Inefficient for large N due to sorting.

πŸ“Š Comparison of Approaches

Approach

⏱️ Time Complexity

πŸ—‚οΈ Space Complexity

⚑ Method

βœ… Pros

⚠️ Cons

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

πŸ’‘ Best Choice?

  • βœ… 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)).

Code (Java)

Code (Python)

Contribution and Support

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! ⭐


πŸ“Visitor Count

Last updated