0% found this document useful (0 votes)
2 views

Sorting Approach

The document describes two approaches to find the k-th largest element in an array: a sorting approach and a min-heap approach. The sorting method sorts the array in descending order, while the min-heap method maintains a heap of the k largest elements for efficiency. Time complexities for the sorting and min-heap approaches are O(n log n) and O((n-k) log k), respectively.

Uploaded by

ishantr573
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sorting Approach

The document describes two approaches to find the k-th largest element in an array: a sorting approach and a min-heap approach. The sorting method sorts the array in descending order, while the min-heap method maintains a heap of the k largest elements for efficiency. Time complexities for the sorting and min-heap approaches are O(n log n) and O((n-k) log k), respectively.

Uploaded by

ishantr573
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sorting Approach

python
Copy
def findKthLargest(nums, k):
# Sort the array in descending order
nums.sort(reverse=True)
# Return the k-th largest element
return nums[k-1]

Explanation:

1. We sort the nums array in descending order using nums.sort(reverse=True).


2. Since the list is now sorted, the k-th largest element is simply at index k-1.
3. Return the element at that index.

Time Complexity:

 Sorting the array takes O(n log n) time, where n is the number of elements in the
array.
 The space complexity is O(1) if we ignore the space used by the sorting algorithm.

Solution 2: Min-Heap Approach

If we use a Min-Heap of size k, we can keep track of the k largest elements in the array
efficiently. In this case, the root of the Min-Heap will always be the smallest of the k largest
elements.

python
Copy
import heapq

def findKthLargest(nums, k):


# Create a min-heap for the first k elements
min_heap = nums[:k]
heapq.heapify(min_heap)

# Process the rest of the elements


for num in nums[k:]:
if num > min_heap[0]: # If current num is larger than the smallest
in the heap
heapq.heappop(min_heap) # Remove the smallest
heapq.heappush(min_heap, num) # Push the new num into the heap

# The root of the heap is the kth largest element


return min_heap[0]

Explanation:

1. First, we create a Min-Heap from the first k elements in the array.


2. Then, for each remaining element in the array, if it's larger than the smallest element
in the heap (min_heap[0]), we replace the smallest element with the new element by
popping and pushing from the heap.
3. After processing all elements, the root of the heap will contain the kth largest element.

Time Complexity:

 Building the initial heap takes O(k) time.


 Each heap operation (pop and push) takes O(log k) time, and we do this for the
remaining n-k elements, so the total time complexity is O((n-k) * log k).
 The space complexity is O(k) due to the heap storage.

Example Walkthrough:

Example 1:

Input:

python
Copy
nums = [3, 2, 1, 5, 6, 4]
k = 2

1. Sorting approach: After sorting the array in descending order, we get [6, 5, 4, 3,
2, 1]. The second largest element is 5, which is at index 1.

Output: 5

2. Min-Heap approach:
o Min-heap after inserting the first k=2 elements: [3, 2]
o After processing the next elements:
 For 1, no change since it's smaller than 3.
 For 5, replace 3 with 5. Min-heap is [5, 2].
 For 6, replace 2 with 6. Min-heap is [5, 6].
 For 4, no change since it's smaller than 5.
o The root of the heap is 5.

You might also like