Skip to content

Commit 2e9cb63

Browse files
authored
Heap DS added
- Added Kth smallest element Kth largest element K sorted array K closest points to origin K frequent elements
1 parent bdd2313 commit 2e9cb63

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

Heap/(1)KthSmallest.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Kth smallest element
5+
priority_queue<int> heap; //max heap
6+
7+
int Solve(int a[], int n, int k) {
8+
9+
for (int i = 0; i < n; i++) {
10+
11+
heap.push(a[i]);
12+
if (heap.size() > k) heap.pop();
13+
14+
}
15+
16+
return heap.top();
17+
}
18+
19+
20+
int main() {
21+
22+
#ifndef ONLINE_JUDGE
23+
freopen("input.txt", "r", stdin);
24+
freopen("output.txt", "w", stdout);
25+
#endif
26+
27+
int n, k;
28+
cin >> n >> k;
29+
int a[n];
30+
for (int i = 0; i < n; i++) cin >> a[i];
31+
32+
cout << Solve(a, n, k) << endl;
33+
}

Heap/(2)KthLargest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Kth largest elements
5+
priority_queue<int, vector<int>, greater<int>> heap; //min heap
6+
7+
void Solve(int a[], int n, int k) {
8+
9+
for (int i = 0; i < n; i++) {
10+
11+
heap.push(a[i]);
12+
if (heap.size() > k) heap.pop();
13+
14+
}
15+
16+
while (heap.size() > 0) {
17+
cout << heap.top() << " ";
18+
heap.pop();
19+
}
20+
}
21+
22+
23+
int main() {
24+
25+
#ifndef ONLINE_JUDGE
26+
freopen("input.txt", "r", stdin);
27+
freopen("output.txt", "w", stdout);
28+
#endif
29+
30+
int n, k;
31+
cin >> n >> k;
32+
int a[n];
33+
for (int i = 0; i < n; i++) cin >> a[i];
34+
35+
Solve(a, n, k);
36+
}

Heap/(3)KSortedArray.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Sort a k-sorted array or nearly sorted array
5+
// Element that should be present in sorted array is within the [i-k,i+k] range
6+
7+
priority_queue<int, vector<int>, greater<int>> heap; //min heap
8+
9+
void Solve(int a[], int n, int k) {
10+
11+
for (int i = 0; i < n; i++) {
12+
13+
heap.push(a[i]);
14+
if (heap.size() > k) {
15+
cout << heap.top() << " ";
16+
heap.pop();
17+
}
18+
19+
}
20+
21+
while (heap.size() > 0) {
22+
cout << heap.top() << " ";
23+
heap.pop();
24+
}
25+
}
26+
27+
28+
int main() {
29+
30+
#ifndef ONLINE_JUDGE
31+
freopen("input.txt", "r", stdin);
32+
freopen("output.txt", "w", stdout);
33+
#endif
34+
35+
int n, k;
36+
cin >> n >> k;
37+
int a[n];
38+
for (int i = 0; i < n; i++) cin >> a[i];
39+
40+
Solve(a, n, k);
41+
}

Heap/(4)KClosestNo.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// K closest numbers to x
5+
6+
priority_queue<pair<int, int>> heap; //max heap
7+
8+
int main() {
9+
10+
#ifndef ONLINE_JUDGE
11+
freopen("input.txt", "r", stdin);
12+
freopen("output.txt", "w", stdout);
13+
#endif
14+
15+
int n, k;
16+
cin >> n >> k;
17+
int x;
18+
cin >> x;
19+
int a[n];
20+
for (int i = 0; i < n; i++) cin >> a[i];
21+
22+
for (int i = 0; i < n; i++) {
23+
heap.push({abs(a[i] - x), a[i]});
24+
if (heap.size() > k) {
25+
heap.pop();
26+
}
27+
}
28+
29+
while (heap.size() > 0) {
30+
cout << heap.top().second << " ";
31+
heap.pop();
32+
}
33+
}

Heap/(5)KFrequent.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Top k frequent numbers
5+
6+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap; // min heap
7+
8+
int main() {
9+
10+
#ifndef ONLINE_JUDGE
11+
freopen("input.txt", "r", stdin);
12+
freopen("output.txt", "w", stdout);
13+
#endif
14+
15+
int n, k;
16+
cin >> n >> k;
17+
int a[n];
18+
unordered_map<int, int> m;
19+
20+
for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; }
21+
22+
for (auto i : m) {
23+
heap.push({i.second, i.first});
24+
25+
if (heap.size() > k) {
26+
heap.pop();
27+
}
28+
29+
}
30+
31+
while (heap.size() > 0) {
32+
cout << heap.top().second << " ";
33+
heap.pop();
34+
}
35+
}

0 commit comments

Comments
 (0)