Last active
October 23, 2021 10:57
-
-
Save janvichhabra/b239e4b06ac90125b27dbe671275fbb7 to your computer and use it in GitHub Desktop.
pepcoding.com 21/10/21 kLargestElements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int findKthLargest(vector<int> const &arr, int k) | |
{ | |
priority_queue<int, vector<int>, greater<int>> pq; | |
for(int i=0;i<k;i++){ | |
pq.push(arr[i]); | |
} | |
for (int i = k; i < arr.size(); i++) | |
{ | |
if (arr[i] > pq.top()) | |
{ | |
pq.pop(); | |
pq.push(arr[i]); | |
} | |
} | |
return pq.top(); | |
} | |
int main() | |
{ | |
vector<int>arr; | |
int n; | |
cin>>n; | |
for(int i= 0 ;i<n;i++){ | |
int data; | |
cin>>data; | |
arr.push_back(data); | |
} | |
int k; | |
cin>>k; | |
for(int i=k;i>0;i--) | |
cout << findKthLargest(arr,i)<<endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def kLargest(arr,size,k): | |
minHeap = [] | |
for i in range(k): | |
minHeap.append(arr[i]) | |
for i in range(k, size): | |
minHeap.sort() | |
if (minHeap[0] > arr[i]): | |
continue | |
else: | |
minHeap.pop(0) | |
minHeap.append(arr[i]) | |
for i in minHeap: | |
print(i) | |
def main(): | |
ints = [] | |
n = int(input()) | |
for i in range(0,n): | |
ele=int(input()) | |
ints.append(ele) | |
size = len(ints) | |
k=int(input()) | |
kLargest(ints, size, k) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <queue> | |
using namespace std; | |
int findKthLargest(vector<int> const &arr, int k){ | |
//write your code here | |
return; | |
} | |
int main() | |
{ | |
vector<int>arr; | |
int n; | |
cin>>n; | |
for(int i= 0 ;i<n;i++){ | |
int data; | |
cin>>data; | |
arr.push_back(data); | |
} | |
int k; | |
cin>>k; | |
for(int i=k;i>0;i--) | |
cout << findKthLargest(arr,i)<<endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def kLargest(arr,size,k): | |
# write your code here | |
def main(): | |
ints = [] | |
n = int(input()) | |
for i in range(0,n): | |
ele=int(input()) | |
ints.append(ele) | |
size = len(ints) | |
k=int(input()) | |
kLargest(ints, size, k) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment