Take FREE!! Online AMCAT Mock Test to Crack Wipro NLTH, Capgemini, etc.
Get Off-Campus Placement Jobs Info !!!
Practice Given Here Wipro NLTH Practice Q&A

Program Discussion :: Basics

Home > Programs > Basics

2 / 259

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: