This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
We can easily solve this problem in 0(n.log(k)) by using a min-heap. The idea is to construct a min-heap of size k and insert the first k elements of array A[0.k-1] into the min-heap. Then for each of the remaining array elements A[k....n-1], if that element is more than the min-heap's root, replace the root with the current element. Repeat this process until the array is exhausted. Now we will be left with top k largest array elements in the min heap, and k'th largest element will reside at the root of the min-heap.
Screenshots(Attach 2 screenshots of your own input and output) -
INPUT:
OUTPUT:
Checklist:
Eg - If your code follow the below guidelines. Kindly change [] to [x]
All the conditions should be fulfilled for considering your code for merging -
[X] I have mentioned the question as comment in my solution file.
[X] My code follows the guidelines of this project.
[X] I have performed a self-review of my own code.
[X] I have commented my code.
[X] My code gives the correct output.
[X] I confirm that I have not copied the code from anywhere. In case its found that I have copied even after successful merge then I can be banned from the repository and hacktoberfest.
[X] I affirm that I strictly follow contributing guidelines and code of conduct.
Issue Id you have worked upon -
167
Briefly explain your program logic -
We can easily solve this problem in 0(n.log(k)) by using a min-heap. The idea is to construct a min-heap of size k and insert the first k elements of array A[0.k-1] into the min-heap. Then for each of the remaining array elements A[k....n-1], if that element is more than the min-heap's root, replace the root with the current element. Repeat this process until the array is exhausted. Now we will be left with top k largest array elements in the min heap, and k'th largest element will reside at the root of the min-heap.
Screenshots(Attach 2 screenshots of your own input and output) -
INPUT:
OUTPUT:
Checklist:
Eg - If your code follow the below guidelines. Kindly change [] to [x]
All the conditions should be fulfilled for considering your code for merging -
[X] I have mentioned the question as comment in my solution file.
[X] My code follows the guidelines of this project.
[X] I have performed a self-review of my own code.
[X] I have commented my code.
[X] My code gives the correct output.
[X] I confirm that I have not copied the code from anywhere. In case its found that I have copied even after successful merge then I can be banned from the repository and hacktoberfest.
[X] I affirm that I strictly follow contributing guidelines and code of conduct.