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

Create Kth largest element.cpp #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Arrays/Kth largest element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>

#include <bits/stdc++.h>

using namespace std;



// Function that returns the Kth largest element

int kth_largest_element(int arr[], int k, int n){

// Sorts the array

sort(arr, arr + n);



// Reverses the array

reverse(arr, arr+n);



// Returns the required element

return arr[k-1];

}



int main(){

// Given array

int arr[] = {12, 15, 7, 3, 8, 16, 25};



// n represents the size of the array

int n = sizeof(arr) / sizeof(arr[0]);



int k = 3;

cout << "The "<< k << "th largest element = " << kth_largest_element(arr, k, n) << endl;

return 0;

}