Last active
October 23, 2021 11:00
-
-
Save janvichhabra/67f55737da5d2f4601f26dd024762862 to your computer and use it in GitHub Desktop.
pepcoding.com 21/10/21 sortK-sortedArray
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 main(){ | |
int n; | |
cin >> n; | |
int arr[n]; | |
for(int i=0;i<n;i++){ | |
cin >> arr[i]; | |
} | |
int k; | |
cin >> k; | |
priority_queue<int,vector<int>,greater<int>> p; | |
for(int i=0;i<=k;i++){ | |
p.push(arr[i]); | |
} | |
for(int i=k+1;i<n;i++){ | |
cout << p.top() << endl; | |
p.pop(); | |
p.push(arr[i]); | |
} | |
while(p.size()>0){ | |
cout << p.top()<< endl; | |
p.pop(); | |
} | |
} |
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
from heapq import heapify,heappush,heappop | |
def sort(l,n,k): | |
heap=l[:k+1] | |
heapify(heap) | |
j=0 | |
for i in range(k+1,n): | |
print(heappop(heap)) | |
heappush(heap,l[i]) | |
while len(heap)!=0: | |
print(heappop(heap)) | |
def main(): | |
ints = [] | |
n = int(input()) | |
for i in range(0,n): | |
ele=int(input()) | |
ints.append(ele) | |
size = len(ints) | |
k=int(input()) | |
sort(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 <bits/stdc++.h> | |
using namespace std; | |
int sortK(int arr[], int n, int k){ | |
//write your code here | |
} | |
int main() | |
{ | |
int n; | |
cin>>n; | |
int arr[n]; | |
for(int i = 0 ; i<n ; i++){ | |
int data; | |
cin>>data; | |
arr[i]=data; | |
} | |
int k; | |
cin>>k; | |
sortK(arr, n, k); | |
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
from heapq import heapify,heappush,heappop | |
def sort(l,n,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()) | |
sort(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