kth smallest/largest in a small range unsorted array
Last Updated :
07 Sep, 2022
Find kth smallest or largest element in an unsorted array, where k<=size of array. It is given that elements of array are in small range.
Examples:
Input : arr[] = {3, 2, 9, 5, 7, 11, 13}
k = 5
Output: 9
Input : arr[] = {16, 8, 9, 21, 43}
k = 3
Output: 16
Input : arr[] = {50, 50, 40}
k = 2
Output: 50
As the given array elements are in a small range, we can direct the index table to do something similar to counting sort. We store counts of elements, then we traverse the count array and print k-th element.
Implementation: Following is the implementation of the above algorithm
C++
// C++ program of kth smallest/largest in
// a small range unsorted array
#include <bits/stdc++.h>
using namespace std;
#define maxs 1000001
int kthSmallestLargest(int* arr, int n, int k)
{
int max_val = *max_element(arr, arr+n);
int hash[max_val+1] = { 0 };
// Storing counts of elements
for (int i = 0; i < n; i++)
hash[arr[i]]++;
// Traverse hash array build above until
// we reach k-th smallest element.
int count = 0;
for (int i=0; i <= max_val; i++)
{
while (hash[i] > 0)
{
count++;
if (count == k)
return i;
hash[i]--;
}
}
return -1;
}
int main()
{
int arr[] = { 11, 6, 2, 9, 4, 3, 16 };
int n = sizeof(arr) / sizeof(arr[0]), k = 3;
cout << "kth smallest number is: "
<< kthSmallestLargest(arr, n, k) << endl;
return 0;
}
Java
// Java program of kth smallest/largest in
// a small range unsorted array
import java.util.Arrays;
class GFG
{
static int maxs = 1000001;
static int kthSmallestLargest(int[] arr, int n, int k)
{
int max_val = Arrays.stream(arr).max().getAsInt();
int hash[] = new int[max_val + 1];
// Storing counts of elements
for (int i = 0; i < n; i++)
{
hash[arr[i]]++;
}
// Traverse hash array build above until
// we reach k-th smallest element.
int count = 0;
for (int i = 0; i <= max_val; i++)
{
while (hash[i] > 0)
{
count++;
if (count == k)
{
return i;
}
hash[i]--;
}
}
return -1;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {11, 6, 2, 9, 4, 3, 16};
int n = arr.length, k = 3;
System.out.println("kth smallest number is: "
+ kthSmallestLargest(arr, n, k));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python 3 program of kth smallest/largest
# in a small range unsorted array
def kthSmallestLargest(arr, n, k):
max_val = arr[0]
for i in range(len(arr)):
if (arr[i] > max_val):
max_val = arr[i]
hash = [0 for i in range(max_val + 1)]
# Storing counts of elements
for i in range(n):
hash[arr[i]] += 1
# Traverse hash array build above until
# we reach k-th smallest element.
count = 0
for i in range(max_val + 1):
while (hash[i] > 0):
count += 1
if (count == k):
return i
hash[i] -= 1
return -1
# Driver Code
if __name__ == '__main__':
arr = [11, 6, 2, 9, 4, 3, 16]
n = len(arr)
k = 3
print("kth smallest number is:",
kthSmallestLargest(arr, n, k))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program of kth smallest/largest in
// a small range unsorted array
using System;
using System.Linq;
class GFG
{
static int maxs = 1000001;
static int kthSmallestLargest(int[] arr, int n, int k)
{
int max_val = arr.Max();
int []hash = new int[max_val + 1];
// Storing counts of elements
for (int i = 0; i < n; i++)
{
hash[arr[i]]++;
}
// Traverse hash array build above until
// we reach k-th smallest element.
int count = 0;
for (int i = 0; i <= max_val; i++)
{
while (hash[i] > 0)
{
count++;
if (count == k)
{
return i;
}
hash[i]--;
}
}
return -1;
}
// Driver code
public static void Main()
{
int []arr = {11, 6, 2, 9, 4, 3, 16};
int n = arr.Length, k = 3;
Console.WriteLine("kth smallest number is: "
+ kthSmallestLargest(arr, n, k));
}
}
/* This code contributed by PrinciRaj1992 */
PHP
<?php
// PHP program of kth smallest/largest in
// a small range unsorted array
$maxs = 1000001;
function kthSmallestLargest(&$arr, $n, $k)
{
global $maxs;
$max_val = max($arr);
$hash = array_fill(0,$max_val+1, NULL);
// Storing counts of elements
for ($i = 0; $i < $n; $i++)
$hash[$arr[$i]]++;
// Traverse hash array build above until
// we reach k-th smallest element.
$count = 0;
for ($i=0; $i <= $max_val; $i++)
{
while ($hash[$i] > 0)
{
$count++;
if ($count == $k)
return $i;
$hash[$i]--;
}
}
return -1;
}
$arr = array ( 11, 6, 2, 9, 4, 3, 16 );
$n = sizeof($arr) / sizeof($arr[0]);
$k = 3;
echo "kth smallest number is: "
. kthSmallestLargest($arr, $n, $k)."\n";
return 0;
?>
JavaScript
<script>
// Javascript program of kth smallest/largest in
// a small range unsorted array
let maxs = 1000001;
function kthSmallestLargest(arr,n,k)
{
let max_val = Math.max(...arr);
let hash = new Array(max_val + 1);
for(let i=0;i<hash.length;i++)
{
hash[i]=0;
}
// Storing counts of elements
for (let i = 0; i < n; i++)
{
hash[arr[i]]++;
}
// Traverse hash array build above until
// we reach k-th smallest element.
let count = 0;
for (let i = 0; i <= max_val; i++)
{
while (hash[i] > 0)
{
count++;
if (count == k)
{
return i;
}
hash[i]--;
}
}
return -1;
}
// Driver code
let arr=[11, 6, 2, 9, 4, 3, 16];
let n = arr.length, k = 3;
document.write("kth smallest number is: "
+ kthSmallestLargest(arr, n, k));
// This code is contributed by avanitrachhadiya2155
</script>
Outputkth smallest number is: 4
Complexity Analysis:
- Time Complexity: O(n + max_val)
- Auxiliary Space: O(n) for hash array.
Similar Reads
Kâth Smallest/Largest Element in Unsorted Array | Expected Linear Time Given an array of distinct integers and an integer k, where k is smaller than the array's size, the task is to find the k'th smallest element in the array.Examples:Input: arr = [7, 10, 4, 3, 20, 15], k = 3Output: 7Explanation: The sorted array is [3, 4, 7, 10, 15, 20], so the 3rd smallest element is
10 min read
K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time) Given an array and a number k where k is smaller than the size of the array, we need to find the kâth largest element in the given array. It is given that all array elements are distinct. We recommend reading the following post as a prerequisite to this post. Kâth Smallest/Largest Element in Unsorte
9 min read
Kâth Smallest/Largest Element in Unsorted Array | Worst case Linear Time Given an array of distinct integers arr[] and an integer k. The task is to find the k-th smallest element in the array. For better understanding, k refers to the element that would appear in the k-th position if the array were sorted in ascending order. Note: k will always be less than the size of t
15 min read
Kâth Smallest Element in Unsorted Array Given an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap
15 min read
How to get largest and smallest number in an Array? Given an array arr[] of length N, The task is to find the maximum and the minimum number in the array. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: Maximum is: 5Minimum is: 1Explanation: The maximum of the array is 5 and the minimum of the array is 1. Input: arr[] = {5, 3, 7, 4, 2}Output: Maximum
15 min read