Skip to content

Commit 259046b

Browse files
committed
Minor changes
1 parent 791807e commit 259046b

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

Arrays/Kth_smallest_element_in_an_array_using_heap.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,32 @@ For Example: arr[] = {7, 8, 1, 4, 9, 3}
1010
This can be solved using heap and priority queue.
1111
***************************************************************************/
1212

13-
SOLUTION (in C++):
13+
// SOLUTION (in C++):
1414

15-
#include<bits/stdc++.h>
15+
#include <bits/stdc++.h>
1616

1717
using namespace std;
1818

1919
int main()
2020
{
2121
int n, k;
22-
cin>>n>>k;
23-
cout<<"Enter the array elements"<<endl;
22+
cin >> n >> k;
23+
cout << "Enter the array elements" << endl;
2424
int arr[n];
25-
priority_queue<int> maxh;
26-
//A priority queue keeps the elements in the order of their priority, i.e.,
25+
priority_queue<int> maxh;
26+
//A priority queue keeps the elements in the order of their priority, i.e.,
2727
//elements having greater values will be at the top of the queue and elements having smaller values will be kept at the bottom of the queue.
2828
//The element popped out of this queue is the element with the maximum value.
2929

30-
for(int i = 0;i<n;i++){
31-
maxh.push(arr[i]);
32-
if(maxh.size() > k){
33-
maxh.pop();
34-
}
30+
for (int i = 0; i < n; i++)
31+
{
32+
maxh.push(arr[i]);
33+
if (maxh.size() > k)
34+
{
35+
maxh.pop();
36+
}
3537
}
36-
37-
cout<<"The "<<k<<"th smallest element is "<<maxh.top();
38+
39+
cout << "The " << k << "th smallest element is " << maxh.top();
3840
return 0;
3941
}

0 commit comments

Comments
 (0)