Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task3 #364

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Task3 #364

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Task1/img/sniggy10.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion Task1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,18 @@ <h5 class="card-title text-center">galacticGhoul</h5>
</div>
</div>
</div>



<div class="col-md-2 col-sm-6 my-12 mx-auto p-relative bg-white shadow-1 blue-hover mycard mycard" style="width: 360px; overflow: hidden; border-radius: 1px;">
<img src="./img/sniggy10.jpg" class="pic w-full">
<div class="card-body">
<h5 class="card-title text-center">Snigdha Ansu</h5>
<div class="text-center">
<a href="https://github.com/sniggy10"><i class="fa fa-github-square icon fa-3x"></i></a>
<a href="https://www.linkedin.com/in/snigdha-ansu-413079173/"><i class="fa fa-linkedin-square fa-3x"></i></a>
</div>
</div>
</div>
<!-- Paste Above this line -->
</div> <!-- end row -->

Expand Down
68 changes: 68 additions & 0 deletions Task3/cpp/K_largest_elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <bits/stdc++.h>

using namespace std;
class Solution{

public:
void swap(int* a,int* b)
{
int temp = *a;
*a=*b;
*b=temp;
}

public:
void max_heapify(int a[],int n,int i){
int largest=i;
int l=2*i+1;
int r=2*i+2;

if(l<n&&a[l]>a[largest])
largest=l;

if(r<n&&a[r]>a[largest])
largest=r;

if(i!=largest){
swap(&a[i],&a[largest]);
max_heapify(a,n,largest);
}
}
public:
vector<int> kLargest(int arr[], int n, int k) {

for(int i=n/2-1;i>=0;i--)
max_heapify(arr,n,i);

vector<int>kl;

for(int i=n-1;i>=n-k;i--)
{
kl.push_back(arr[0]);
swap(&arr[0],&arr[i]);
max_heapify(arr,i,0);
}
return kl;
}

};

int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
Solution ob;
auto ans = ob.kLargest(arr, n, k);
for (auto x : ans) {
cout << x << " ";
}
cout << "\n";
}
return 0;
}