Find the "Kth" max and min element of an array .

Given an array arr[] and a number K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.

Expected Time Complexity: O(n)


//Here we have a function that gives the kth smallest element.


1:  int kthSmallest(int arr[], int n, int k)   
2:  {  
3:   
4:    sort(arr,arr+n);  
5:    return(arr[k-1]);  
6:  }   


//Here we have a function that gives the kth largest element.

1:  int kthSmallest(int arr[], int n, int k)   
2:  {  
3:      
4:    sort(arr,arr+n,greater());  
5:    return(arr[k-1]);  
6:  }  

Post a Comment

0 Comments