[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Basics

Home > Programs > Basics

101 / 279

Given an unsorted array and an item K, write a program to get kth largest element.

Answer:

#include
#include
using namespace std; 
/* Function to return k'th smallest element in a given array */
int kthSmallest(int arr[], int n, int k)
{
    /* Sort the given array */
    sort(arr, arr+n);
 
    /* Return k'th element in the sorted array */
    return arr[k-1];
}
 
/* Driver program to test above methods */
int main()
{
    int arr[] = {12, 3, 5, 7, 19};
    int n = sizeof(arr)/sizeof(arr[0]), k = 2;
    cout 

Asked In :: Adobe

Post Your Answer Here:

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: