

Find Kth Largest/Smallest element in a Queue
source link: https://www.geeksforgeeks.org/find-kth-largest-smallest-element-in-a-queue/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find Kth Largest/Smallest element in a Queue
Given a queue of integers and an integer K, our task is to write a program that efficiently finds Kth largest/smallest element present in the queue.Return -1 if the number of unique elements inside the queue is less than K.
Examples:
Input: Queue = {15, 27, 18, 13}, K = 2
Output: 18
Explanation: Among 15(front), 27, 18 and 13(back), 18 is the second(Kth) largest.Input: Queue = {12, 25, 29, 16, 32}, K = 3
Output: 25
Explanation: Among 12(front), 25, 29, 16 and 32(back), 25 is the third(Kth) largest.
Approach: To solve the problem follow the below idea:
The idea to solve the problem is to insert all the queue elements into a set. And then return the K’th largest/smallest element from the set.
Follow the given steps to solve the problem:
- First, check if the size of the queue is less than K. If so, it returns -1, as it is not possible to find the K’th largest/smallest unique element in a queue with less than K elements.
- Now, store every element of the queue into the set st from the queue while popping items from it.
- Now, if K is greater than the size of the set st then return -1, as the total number of unique elements is less than K.
- Create a variable ind for index and initialize it by 1.
- Now traverse over sorted elements in the set st and check if the current element index is equal to K (for smallest) and N+1-K (for largest), where N is the size of the set st.
- If Index matches, return the value at that index.
Below is the implementation of the above approach to find K’th largest:
- Python3
// C++ program to find the Kth largest element // present in the queue #include <bits/stdc++.h> using namespace std; // Function which return Kth largest element // of the queue int kElement(queue<int> q, int K) { // If queue size is less than K // then return -1 if (q.size() < K) return -1; // Declare SET st set<int> st; // Loop to insert every element of queue // inside set while (!q.empty()) { // Inserting current front element // inside set st.insert(q.front()); // pop current front element q.pop(); } // To store set size/length int N = st.size(); // Check if set size is less than K if (N < K) return -1; // Initialize index by 1 int ind = 1; // Loop to find K'th largest element for (auto it = st.begin(); ind <= N; it++) { // If sorted index is equal to k // return element of that index if (ind == (N - K + 1)) return *it; // Increment the index by 1 ind++; } } // Driver Code int main() { queue<int> q; // Pushing elements into queue q.push(15); q.push(27); q.push(18); q.push(13); int K = 2; // Call function and store return value // into kMaxx int kMaxx = kElement(q, K); // Print the Kth largest element cout << "The " << K << "th largest element is " << kMaxx << endl; return 0; }
The 2th largest element is 18
Time Complexity: O(N*logN), to insert all the elements into the set.
Auxiliary Space: O(N), to store all elements into the set.
Recommend
-
9
Find kth Smallest and Largest Element in an Array in C++ Hello everyone, in this post we are going to go through a very popular and recently asked coding question. Finding the kth smallest and larges...
-
12
Leetcode 230. Kth Smallest Element in a BST 当前位置:XINDOO > 算法 > 正文 Given a binary sea...
-
7
-
5
Java Program to Find Largest Element in an ArrayJava Program to Find Largest Element in an Array110 Views01/08/2022In this video,...
-
2
本文总结经典的区间第k小值数据结构题。 给定一个长为n的数组,元素为范围为[0,σ)的整数。有m个询问:求区间[l,r)中第k小的元素。 一些方法支持扩展问题:有m个操作,或者修改某个位置上的元素,或者询问区间[l,r)中第k小的元素。
-
8
Finding largest palindromic number divisible by smallest primeGiven a range [start, end], find the largest palindromic number in the range and check if it is divisible by the
-
10
Find the XOR of smallest and largest triangular number in a given rangeGiven a range, find the XOR of the smallest and largest triangular numbers within that range.
-
6
JavaScript Program to Find Largest Element in an ArraySkip to content
-
6
Javascript Program to Find the Largest Element in an ArraySkip to content
-
1
Compressed String decoding for Kth characterGiven a compressed string composed of lowercase characters and numbers, the task is to decode the compressed string and to find out the character lo...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK