## K’th Smallest/Largest Element in Unsorted Array

Given an array and a number k where k is smaller than size of array, we need to find the k’th smallest element in the given array. It is given that ll array elements are distinct.

Examples:

```Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 3
Output: 7

Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 4
Output: 10```

### C/C++

`// Simple C++ program to find k'th smallest element#include<iostream>#include<algorithm>using namespace std;// Function to return k'th smallest element in a given arrayint kthSmallest(int arr[], int n, int k){// Sort the given arraysort(arr, arr+n);// Return k'th element in the sorted arrayreturn arr[k-1];}// Driver program to test above methodsint main(){int arr[] = {12, 3, 5, 7, 19};int n = sizeof(arr)/sizeof(arr[0]), k = 2;cout << "K'th smallest element is " << kthSmallest(arr, n, k);return 0;}`

### Java

`// Java code for kth smallest element // in an arrayimport java.util.Arrays;import java.util.Collections;class GFG{ // Function to return k'th smallest // element in a given arraypublic static int kthSmallest(Integer [] arr, int k) {// Sort the given arrayArrays.sort(arr);// Return k'th element in // the sorted arrayreturn arr[k-1];} // driver programpublic static void main(String[] args) {Integer arr[] = new Integer[]{12, 3, 5, 7, 19};int k = 2;System.out.print( "K'th smallest element is "+kthSmallest(arr, k) ); }}`