Kth smallest element in the array using constant space when array can’t be modified
Last Updated :
05 Dec, 2022
Given an array arr[] of size N having no duplicates and an integer K, the task is to find the Kth smallest element from the array in constant extra space and the array can’t be modified.
Examples:
Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3
Output: 7
Given array in sorted is {3, 4, 7, 10, 15, 20}
where 7 is the third smallest element.
Input: arr[] = {12, 3, 5, 7, 19}, K = 2
Output: 5
Approach: First we find the min and max elements from the array. Then we set low = min, high = max and mid = (low + high) / 2.
Now, perform a modified binary search, and for each mid we count the number of elements less than mid and equal to mid. If countLess < k and countLess + countEqual ? k then mid is our answer, else we have to modify our low and high.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int kthSmallest( int * arr, int k, int n)
{
int low = *min_element(arr, arr + n);
int high = *max_element(arr, arr + n);
while (low <= high) {
int mid = low + (high - low) / 2;
int countless = 0, countequal = 0;
for ( int i = 0; i < n; ++i) {
if (arr[i] < mid)
++countless;
else if (arr[i] == mid)
++countequal;
}
if (countless < k
&& (countless + countequal) >= k) {
return mid;
}
else if (countless >= k) {
high = mid - 1;
}
else if (countless < k
&& countless + countequal < k) {
low = mid + 1;
}
}
}
int main()
{
int arr[] = { 7, 10, 4, 3, 20, 15 };
int n = sizeof (arr) / sizeof ( int );
int k = 3;
cout << kthSmallest(arr, k, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int kthSmallest( int [] arr, int k, int n)
{
int low = Arrays.stream(arr).min().getAsInt();
int high = Arrays.stream(arr).max().getAsInt();
while (low <= high)
{
int mid = low + (high - low) / 2 ;
int countless = 0 , countequal = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if (arr[i] < mid)
++countless;
else if (arr[i] == mid)
++countequal;
}
if (countless < k
&& (countless + countequal) >= k)
{
return mid;
}
else if (countless >= k)
{
high = mid - 1 ;
}
else if (countless < k
&& countless + countequal < k)
{
low = mid + 1 ;
}
}
return Integer.MIN_VALUE;
}
public static void main(String[] args)
{
int arr[] = { 7 , 10 , 4 , 3 , 20 , 15 };
int n = arr.length;
int k = 3 ;
System.out.println(kthSmallest(arr, k, n));
}
}
|
Python3
def kthSmallest(arr, k, n) :
low = min (arr);
high = max (arr);
while (low < = high) :
mid = low + (high - low) / / 2 ;
countless = 0 ; countequal = 0 ;
for i in range (n) :
if (arr[i] < mid) :
countless + = 1 ;
elif (arr[i] = = mid) :
countequal + = 1 ;
if (countless < k and (countless + countequal) > = k) :
return mid;
elif (countless > = k) :
high = mid - 1 ;
elif (countless < k and countless + countequal < k) :
low = mid + 1 ;
if __name__ = = "__main__" :
arr = [ 7 , 10 , 4 , 3 , 20 , 15 ];
n = len (arr);
k = 3 ;
print (kthSmallest(arr, k, n));
|
C#
using System;
using System.Linq;
class GFG
{
static int kthSmallest( int [] arr, int k, int n)
{
int low = arr.Min();
int high = arr.Max();
while (low <= high)
{
int mid = low + (high - low) / 2;
int countless = 0, countequal = 0;
for ( int i = 0; i < n; ++i)
{
if (arr[i] < mid)
++countless;
else if (arr[i] == mid)
++countequal;
}
if (countless < k
&& (countless + countequal) >= k)
{
return mid;
}
else if (countless >= k)
{
high = mid - 1;
}
else if (countless < k
&& countless + countequal < k)
{
low = mid + 1;
}
}
return int .MinValue;
}
public static void Main(String[] args)
{
int []arr = { 7, 10, 4, 3, 20, 15 };
int n = arr.Length;
int k = 3;
Console.WriteLine(kthSmallest(arr, k, n));
}
}
|
Javascript
<script>
function kthSmallest(arr, k, n) {
let temp = [...arr];
let low = temp.sort((a, b) => a - b)[0];
let high = temp[temp.length - 1];
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
let countless = 0, countequal = 0;
for (let i = 0; i < n; ++i) {
if (arr[i] < mid)
++countless;
else if (arr[i] == mid)
++countequal;
}
if (countless < k
&& (countless + countequal) >= k) {
return mid;
}
else if (countless >= k) {
high = mid - 1;
}
else if (countless < k
&& countless + countequal < k) {
low = mid + 1;
}
}
}
let arr = [7, 10, 4, 3, 20, 15];
let n = arr.length;
let k = 3;
document.write(kthSmallest(arr, k, n));
</script>
|
Time Complexity: O(N log(Max – Min)) where Max and Min are the maximum and minimum elements from the array respectively and N is the size of the array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...