## Kth Largest Element in a Stream Leetcode Solution

Problem Statement In this problem, we have to design a class KthLargest() that initially has an integer k and an array of integers. We need to write a parameterized constructor for it when an integer k and array nums are passed as arguments. The class also has a function add(val) that adds …

## Kth largest element in an Array Leetcode Solutions

In this problem, we have to return the kth largest element in an unsorted array. Note that the array can have duplicates. So, we have to find the Kth largest element in the sorted order, not the distinct Kth largest element. Example A = {4 , 2 , 5 , 3 …

## Top K Frequent Elements

Problem Statement In top K frequent elements we have given an array nums[], find the k most frequently occurring elements. Examples nums[] = {1, 1, 1, 2, 2, 3} k = 2 1 2   nums[] = {1} k = 1 1 Naive Approach for Top K Frequent Elements Build …

## Convert BST to Min Heap

Problem Statement Given a complete Binary Search Tree, write an algorithm to convert it into a Min Heap, which is to convert BST to Min Heap. The Min Heap should be such that the values on the left of a node must be less than the values on the right …

## Super Ugly Number

Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. Note:  1 is considered to be the first super ugly number. Approach 1: Brute force Main idea We will iterate …

## K-th Distinct Element in an Array

You are given an integer array A, print k-th distinct element in an array. The given array may contain duplicates and the output should print k-th distinct element among all unique elements in an array. If k is more than a number of distinct elements, then report it. Example Input: …

## Top K Frequent Words

In top K frequent words problem, we have given a list of words and an integer k. Print k most frequently used strings in the list. Example Input : list = {“code”, “sky”, “pen”, “sky”, “sky”, “blue”, “code”} k = 2 Output :  sky code Input : list = {“yes”, …

## Split Array Into Consecutive Subsequences

Given a sorted array(in ascending order), check if the array can be split into 1 or more subsequences of length greater than equals to 3 such that every subsequence contains consecutive numbers. Examples Input: arr[] = {1,2,3,3,4,5} Output: true Explanation: The array can be split into 2 subsequences as, sub1[] …

## Minimum Cost to Hire K Workers

In minimum cost to hire K workers problem, we have given N workers from which we want to hire exactly k workers to form a paid group.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Pay will be given to them according to the following rules: …

## How to Implement Stack Using Priority Queue or Heap?

Implement a stack with the help of a priority queue or a heap. Priority Queue : Priority queue data structure is similar to the queue or stack data structure with an addition of priority. Every element is given a priority number. In conclusion, the elements with high priority are prefered …