Dsa
Arrays
Hard
Kth Largest Distinct Element
You are given an array of integers. Your task is to find the Kth largest distinct element in the array. Return -1 if there are fewer than K distinct elements.
Input format:
- The first line contains two integers n (1 ≤ n ≤ 10^5) and k (1 ≤ k ≤ n).
- The second line contains n space-separated integers a[i] (0 ≤ a[i] ≤ 10^9).
Output format:
- A single integer - the Kth largest distinct element or -1 if it does not exist.
Example:
Input:
6 2
4 3 5 5 1 2
Output:
3
Key concepts
arrayssortingdistinct
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now