K’th Smallest/Largest Element using STL
Last Updated :
08 Feb, 2023
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int kthSmallest( int arr[], int n, int k)
{
set< int > s;
for ( int i = 0; i < n; i++)
s.insert(arr[i]);
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;
}
|
Java
import java.util.Set;
import java.util.TreeSet;
public class KthSmallest {
public static int kthSmallest( int [] arr, int n, int k) {
Set<Integer> s = new TreeSet<Integer>();
for ( int i = 0 ; i < n; i++) {
s.add(arr[i]);
}
int i = 0 ;
for (Integer num : s) {
if (i == k - 1 ) {
return num;
}
i++;
}
return - 1 ;
}
public static void main(String[] args) {
int [] arr = { 12 , 3 , 5 , 7 , 3 , 19 };
int n = arr.length, k = 2 ;
System.out.println( "K'th smallest element is " + kthSmallest(arr, n, k));
}
}
|
Python3
from sortedcontainers import SortedList, SortedSet, SortedDict
def kthSmallest(arr, n, k) :
s = SortedSet();
for i in range ( 0 ,n):
s.add(arr[i]);
i = 1 ;
ans = - 1 ;
for val in s:
if (i = = k):
ans = val;
break ;
i + = 1 ;
return ans;
arr = [ 12 , 3 , 5 , 7 , 3 , 19 ];
n = len (arr);
k = 2 ;
print ( "K'th smallest element is " ,kthSmallest(arr, n, k));
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class KthSmallest
{
public static int kthSmallest( int [] arr, int k)
{
var s = new SortedSet< int >(arr);
return s.ElementAt(k - 1);
}
public static void Main( string [] args)
{
int [] arr = { 12, 3, 5, 7, 3, 19 };
int k = 2;
Console.WriteLine( "K'th smallest element is " + kthSmallest(arr, k));
}
}
|
Javascript
function kthSmallest(arr, n, k)
{
let s= new Set();
for (let i = 0; i < n; i++)
s.add(arr[i]);
let i = 0;
for (const entry of s.values())
{
if (i==k)
{
i=entry;
break ;
}
i++;
}
return i;
}
let arr = [ 12, 3, 5, 7, 3, 19 ];
let n = arr.length, k = 2;
document.write( "K'th smallest element is " + kthSmallest(arr, n, k));
|
Output
K'th smallest element is 5
Time complexity: 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).
Auxiliary Space: O(n) where n is size of array, since n extra space has been taken.
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...