In this task, you need to use 2 priority queues to print out the kth largest and jth smallest number in 2 new different random vectors, vector3 and vector4. Write a function called int kthLargestPQ( vector& vec, int k), and a function called int jthSmallestPQ(vector& vec, int k). Here is an example: Original vector3: 9 8 9 2 3 79577 Input K for Kth largest: 3 The 3 largest number: 9 Original vector4: 9 6 5 6 7 76 162 Input J for Jth smallest: 3 The 3 smallest number: 5 Hint: Using priority queue. This problem can be solved in two ways. 1. For the kthLargestPQ, we can maintain a small heap (small priority queue, smallest element in the top) with only k elements in size. After traversing all the elements, the top element in the PQ is what we want. The jthSmallestPQ vice versa. 2. To get the kth largest element, you can construct a large priority queue using all the elements, pop the top element k-1 times, then the top element is what we want. To get the jth smallest element, you

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How does one use an priority queue to solve this type of question? 

In this task, you need to use 2 priority queues to print out the kth largest and jth smallest number in 2
new different random vectors, vector3 and vector4. Write a function called int
kthLargestPQ(vector<int>& vec, int k), and a function called int jthSmallestPQ(vector<int>& vec,
int k).
Here is an example:
Original vector3: 9 8 9 2 3 7 9 577
Input K for Kth largest: 3
The largest number: 9
Original vector4: 9 6 5 6 7 7 6 16 2
Input J for Jth smallest: 3
The 3 smallest number: 5
Hint:
Using priority queue.
This problem can be solved in two ways.
1.
For the kthLargestPQ, we can maintain a small heap (small priority queue, smallest element in the
top) with only k elements in size. After traversing all the elements, the top element in the PQ is what we
want.
The jthSmallestPQ vice versa.
2. To get the kth largest element, you can construct a large priority queue using all the elements, pop
the top element k-1 times, then the top element is what we want. To get the jth smallest element, you
Transcribed Image Text:In this task, you need to use 2 priority queues to print out the kth largest and jth smallest number in 2 new different random vectors, vector3 and vector4. Write a function called int kthLargestPQ(vector<int>& vec, int k), and a function called int jthSmallestPQ(vector<int>& vec, int k). Here is an example: Original vector3: 9 8 9 2 3 7 9 577 Input K for Kth largest: 3 The largest number: 9 Original vector4: 9 6 5 6 7 7 6 16 2 Input J for Jth smallest: 3 The 3 smallest number: 5 Hint: Using priority queue. This problem can be solved in two ways. 1. For the kthLargestPQ, we can maintain a small heap (small priority queue, smallest element in the top) with only k elements in size. After traversing all the elements, the top element in the PQ is what we want. The jthSmallestPQ vice versa. 2. To get the kth largest element, you can construct a large priority queue using all the elements, pop the top element k-1 times, then the top element is what we want. To get the jth smallest element, you
2. To get the kth largest element, you can construct a large priority queue using all the elements, pop
the top element k-1 times, then the top element is what we want. To get the jth smallest element, you
can construct a small priority queue using all the elements, pop the top element j-1 times, then the top
element is what we want.
Transcribed Image Text:2. To get the kth largest element, you can construct a large priority queue using all the elements, pop the top element k-1 times, then the top element is what we want. To get the jth smallest element, you can construct a small priority queue using all the elements, pop the top element j-1 times, then the top element is what we want.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education