# K’th Smallest/Largest Element using STL

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.

Examples:

Input : arr[] = {7, 10, 4, 3, 20, 15} k = 2 Output : 4 Smallest element is 3. Second smallest is 4. Input : arr[] = {7, 10, 4, 3, 3, 15} k = 2 Output : 4 Even if there are more than one occurrences of 3, answer should be 4. Input :arr[] = {7, 10, 4, 3, 20, 15} k = 4 Output : 10

We use set in C++ STL.

1) Insert all elements into a set.

2) Traverse the set and print k-th element.

`// STL based C++ program to find k-th smallest ` `// element. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `kthSmallest(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Insert all elements into the set ` ` ` `set<` `int` `> s; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `s.insert(arr[i]); ` ` ` ` ` `// Traverse set and print k-th element ` ` ` `auto` `it = s.begin(); ` ` ` `for` `(` `int` `i = 0; i < k - 1; i++) ` ` ` `it++; ` ` ` `return` `*it; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 12, 3, 5, 7, 3, 19 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]), k = 2; ` ` ` `cout << ` `"K'th smallest element is "` ` ` `<< kthSmallest(arr, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

K'th smallest element is 5

Time complexity of above solution is O(n Log n). Note that set in STL uses a self-balancing BST internally and therefore time complexity of search and insert operations is O(log n).

**Related Posts : **

K’th Smallest/Largest Element in Unsorted Array | Set 1

K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time

K’th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)

This article is contributed by **Roshan Halwai**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Replace elements with absolute difference of smallest element on left and largest element on right
- Replace each element by the difference of the total size of the array and frequency of that element
- Range Query on array whose each element is XOR of index value and previous element
- Replace every element with the greatest element on its left side
- Longest Subarray with first element greater than or equal to Last element
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Find last element after deleting every second element in array of n integers
- Replace every array element by Bitwise Xor of previous and next element
- Maximum possible remainder when an element is divided by other element in the array
- Replace every element with the smallest element on its left side
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Replace every element of the array by its previous element
- Replace every element with the greatest element on right side
- Replace every element of the array by its next element
- Replace every element with the least greater element on its right