Question: Part 2 : Kth largest / smallest element finder For part two, you have to write a method to return the kth largest or smallest
Part : Kth largestsmallest element finder
For part two, you have to write a method to return the kth largest or smallest element from a list of n elements, where n is several times larger than k An obvious approach to finding the Kth largestsmallest element would be to sort them, which requires Onlogn operations. However, this approach becomes inefficient in both time and space with increase in input size. Heaps, on the other hand, provide a much better solution. The runtime complexity with heaps is Onlogk Your task is to come with an algorithm to find the kth largest or smallest element using heaps in Onlogk
Given a list of n nonnegative integers in the form of a file, the Kthfinder method would return the Kth largestsmallest number from the input file. File name, value of K and the type of the task largest or smallest are the arguments to the function. This function is present inside the ElementFinder class.
For example, input.txt is a file that contains numbers with spaceseparated numbers in each line. The method Kthfinderinputtxtlargest would return
Required Method Description
Method Name Description
public Kthfinderfilename K operation Return the Kth largest or Kth smallest element
Parameters:
filename String type. Make sure to add proper checks and trycatch conditions.
K You can expect K to always be smaller than the size of the input and greater than
operation Either largest or smallest
Reading the file: You should read one line at the time, evaluate all the numbers in that line and then read the next line. You should not load the entire file at once. You can expect test cases where the file size is bigger than the available memory.
Return value: The method should return the kth largestsmallest element if it exists. If no such element exists, the method should return
Algorithm: There are four steps to this algorithm:
First, figure out the type of heap MinHeap or MaxHeap you will need depending upon the type of operation. Theres no programming involved in this step.
Second, implement the comparators needed for each type of heap. Take a look at the unit test for help.
The third step is to create a heap that allows you to find the required element in Onlogk complexity. What it means is that the bubbleUpbubbleDown operations should only take about Ologk Think of the input file as an infinite sequence of numbers. Theres no possible way to store all of them. But we can store upto K elements in the form of a heap. So far the complexity is only Oklogk
The last step is to use this heap in a way that for every element after the first k youd need at most Ologk operations to figure out if this element should be discarded or stored in the heap.For part two, you have to write a method to return the kth largest or smallest element from a list of
elements, where is several times larger than k An obvious approach to finding the Kth
largestsmallest element would be to sort them, which requires Onlogn operations. However, this
approach becomes inefficient in both time and space with increase in input size. Heaps, on the other
hand, provide a much better solution. The runtime complexity with heaps is nlogk Your task is to
come with an algorithm to find the kth largest or smallest element using heaps in nlogk
Given a list of nonnegative integers in the form of a file, the thfinder method would return the
Kth largestsmallest number from the input file. File name, value of and the type of the task
largest or "smallest" are the arguments to the function. This function is present inside the
ElementFinder class.
For example, input.txt is a file that contains numbers with spaceseparated numbers in each line.
The method Kthfinderinputtxt "largest" would return
Required Method Description
Method Name
public Kthfinderfilename K
operation
Parameters:
Description
Return the Kth largest or Kth smallest
element
filename String type. Make sure to add proper checks and trycatch conditions.
You can expect to always be smaller than the size of the input and greater than
operation Either "largest" or "smallest"
Reading the file: You should read one line at the time, evaluate all the numbers in that line and then
read the next line. You should not load the entire file at once. You can expect test cases where the file
size is bigger than the available memory.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
