DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Kth Largest Element in an Array

Added two solution
1 -> using MaxPriorityQueue
2 -> using MinPriorityQueue

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findKthLargest = function (nums, k) {
  // using in built function of Max heap via Max Priority queue
  const elements = new MaxPriorityQueue();
  for (let i = 0; i < nums.length; i++) {
    elements.enqueue(nums[i]);
  }
  let kthLargest = -Infinity;

  // as heap se sorted Descending order we can just remove top k elements
  while (k) {
    kthLargest = elementss.dequeue().element;
    k--;
  }

  return kthLargest;
};

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findKthLargest = function (nums, k) {
  // using in built function of Max heap via Min Priority queue
  const heap = new MinPriorityQueue();

  // as heap se sorted asceding order we can just keep track that we have max k element in queue
  // reason is at last of traversal when we take top most element it will be the kth largest as we have only k elements in heap
  // as soon as size goes beyond k we use dequeue to remove element
  for (let i = 0; i < nums.length; i++) {
    if (heap.size() == k && nums[i] <= heap.front()?.element) continue;
    heap.enqueue(nums[i]);
    if (heap.size() > k) heap.dequeue();
  }

  // return top most
  return heap.front().element;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)