C# Program to Find kth Largest Element
You can find the kth largest element in C# by sorting the array and accessing the element at index
array.Length - k, for example: Array.Sort(arr); int kthLargest = arr[arr.Length - k];.Examples
Input[3, 1, 5, 12, 2, 11], k=3
Output5
Input[5, 12, 11, -1, 12], k=2
Output12
Input[1], k=1
Output1
How to Think About It
To find the kth largest element, first sort the array in ascending order so the largest elements are at the end. Then, pick the element at the position that is k places from the end, which is at index
length - k. This works because sorting arranges elements from smallest to largest.Algorithm
1
Get the input array and the value k.2
Sort the array in ascending order.3
Calculate the index as array length minus k.4
Return the element at that index.Code
csharp
using System; class Program { static void Main() { int[] arr = {3, 1, 5, 12, 2, 11}; int k = 3; Array.Sort(arr); int kthLargest = arr[arr.Length - k]; Console.WriteLine(kthLargest); } }
Output
5
Dry Run
Let's trace the example array [3, 1, 5, 12, 2, 11] with k=3 through the code.
1
Input array and k
arr = [3, 1, 5, 12, 2, 11], k = 3
2
Sort the array
arr after sorting = [1, 2, 3, 5, 11, 12]
3
Calculate index
index = arr.Length - k = 6 - 3 = 3
4
Get kth largest element
arr[3] = 5
| Step | Array State | Index Calculated | Element Selected |
|---|---|---|---|
| Initial | [3, 1, 5, 12, 2, 11] | - | - |
| After Sort | [1, 2, 3, 5, 11, 12] | - | - |
| Calculate Index | - | 3 | - |
| Select Element | - | - | 5 |
Why This Works
Step 1: Sorting the array
Sorting arranges elements from smallest to largest, so the largest elements are at the end.
Step 2: Finding the kth largest index
The kth largest element is at index arr.Length - k because array indices start at 0.
Step 3: Accessing the element
Accessing the element at this index gives the kth largest value directly.
Alternative Approaches
Using LINQ OrderByDescending
csharp
using System; using System.Linq; class Program { static void Main() { int[] arr = {3, 1, 5, 12, 2, 11}; int k = 3; int kthLargest = arr.OrderByDescending(x => x).ElementAt(k - 1); Console.WriteLine(kthLargest); } }
This method uses LINQ to sort descending and picks the (k-1)th element; it's concise but may be less efficient for large arrays.
Using a Min-Heap (Priority Queue)
csharp
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {3, 1, 5, 12, 2, 11}; int k = 3; var minHeap = new SortedSet<(int val, int idx)>(); int idx = 0; foreach (var num in arr) { minHeap.Add((num, idx++)); if (minHeap.Count > k) { minHeap.Remove(minHeap.Min); } } Console.WriteLine(minHeap.Min.val); } }
This approach keeps only k largest elements in a min-heap, efficient for large data but more complex to implement.
Complexity: O(n log n) time, O(1) space
Time Complexity
Sorting the array takes O(n log n) time, which dominates the runtime.
Space Complexity
Sorting in-place uses O(1) extra space; no additional data structures are needed.
Which Approach is Fastest?
Using a min-heap can reduce time to O(n log k) for large n and small k, but sorting is simpler and efficient for small to medium arrays.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Sorting and indexing | O(n log n) | O(1) | Small to medium arrays, simplicity |
| LINQ OrderByDescending | O(n log n) | O(n) | Concise code, readability |
| Min-Heap (Priority Queue) | O(n log k) | O(k) | Large arrays with small k, performance |
Sort the array and pick the element at index length minus k for a simple solution.
Forgetting that array indices start at 0 and using k directly as the index.