Find the kth element in the series generated by the given N ranges
Given N non-overlapping ranges L[] and R[] where the every range starts after the previous range ends i.e. L[i] > R[i – 1] for all valid i. The task is to find the Kth element in the series which is formed after sorting all the elements in all the given ranges in ascending order.
Examples:
Input: L[] = {1, 8, 21}, R[] = {4, 10, 23}, K = 6
Output: 9
The generated series will be 1, 2, 3, 4, 8, 9, 10, 21, 22, 23
And the 6th element is 9
Input: L[] = {2, 11, 31}, R[] = {7, 15, 43}, K = 13
Output: 32
Approach: The idea is to use binary search. An array total to store the number of integers that are present upto ith index, now with the help of this array find out the index in which the kth integer will lie. Suppose that index is j, now compute the position of the kth smallest integer in the interval L[j] to R[j] and find the kth smallest integer using binary search where low will be L[j] and high will be R[j].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getKthElement( int n, int k, int L[], int R[])
{
int l = 1;
int h = n;
int total[n + 1];
total[0] = 0;
for ( int i = 0; i < n; i++) {
total[i + 1] = total[i] + (R[i] - L[i]) + 1;
}
int index = -1;
while (l <= h) {
int m = (l + h) / 2;
if (total[m] > k) {
index = m;
h = m - 1;
}
else if (total[m] < k)
l = m + 1;
else {
index = m;
break ;
}
}
l = L[index - 1];
h = R[index - 1];
int x = k - total[index - 1];
while (l <= h) {
int m = (l + h) / 2;
if ((m - L[index - 1]) + 1 == x) {
return m;
}
else if ((m - L[index - 1]) + 1 > x)
h = m - 1;
else
l = m + 1;
}
}
int main()
{
int L[] = { 1, 8, 21 };
int R[] = { 4, 10, 23 };
int n = sizeof (L) / sizeof ( int );
int k = 6;
cout << getKthElement(n, k, L, R);
return 0;
}
|
Java
class GFG
{
static int getKthElement( int n, int k,
int L[], int R[])
{
int l = 1 ;
int h = n;
int total[] = new int [n + 1 ];
total[ 0 ] = 0 ;
for ( int i = 0 ; i < n; i++)
{
total[i + 1 ] = total[i] +
(R[i] - L[i]) + 1 ;
}
int index = - 1 ;
while (l <= h)
{
int m = (l + h) / 2 ;
if (total[m] > k)
{
index = m;
h = m - 1 ;
}
else if (total[m] < k)
l = m + 1 ;
else
{
index = m;
break ;
}
}
l = L[index - 1 ];
h = R[index - 1 ];
int x = k - total[index - 1 ];
while (l <= h)
{
int m = (l + h) / 2 ;
if ((m - L[index - 1 ]) + 1 == x)
{
return m;
}
else if ((m - L[index - 1 ]) + 1 > x)
h = m - 1 ;
else
l = m + 1 ;
}
return k;
}
public static void main(String[] args)
{
int L[] = { 1 , 8 , 21 };
int R[] = { 4 , 10 , 23 };
int n = L.length;
int k = 6 ;
System.out.println(getKthElement(n, k, L, R));
}
}
|
Python3
def getKthElement(n, k, L, R):
l = 1
h = n
total = [ 0 for i in range (n + 1 )]
total[ 0 ] = 0
for i in range (n):
total[i + 1 ] = total[i] + (R[i] - L[i]) + 1
index = - 1
while (l < = h):
m = (l + h) / / 2
if (total[m] > k):
index = m
h = m - 1
elif (total[m] < k):
l = m + 1
else :
index = m
break
l = L[index - 1 ]
h = R[index - 1 ]
x = k - total[index - 1 ]
while (l < = h):
m = (l + h) / / 2
if ((m - L[index - 1 ]) + 1 = = x):
return m
elif ((m - L[index - 1 ]) + 1 > x):
h = m - 1
else :
l = m + 1
L = [ 1 , 8 , 21 ]
R = [ 4 , 10 , 23 ]
n = len (L)
k = 6
print (getKthElement(n, k, L, R))
|
C#
using System;
class GFG
{
static int getKthElement( int n, int k,
int [] L, int [] R)
{
int l = 1;
int h = n;
int [] total = new int [n + 1];
total[0] = 0;
for ( int i = 0; i < n; i++)
{
total[i + 1] = total[i] +
(R[i] - L[i]) + 1;
}
int index = -1;
while (l <= h)
{
int m = (l + h) / 2;
if (total[m] > k)
{
index = m;
h = m - 1;
}
else if (total[m] < k)
l = m + 1;
else
{
index = m;
break ;
}
}
l = L[index - 1];
h = R[index - 1];
int x = k - total[index - 1];
while (l <= h)
{
int m = (l + h) / 2;
if ((m - L[index - 1]) + 1 == x)
{
return m;
}
else if ((m - L[index - 1]) + 1 > x)
h = m - 1;
else
l = m + 1;
}
return k;
}
public static void Main()
{
int [] L = { 1, 8, 21 };
int [] R = { 4, 10, 23 };
int n = L.Length;
int k = 6;
Console.WriteLine(getKthElement(n, k, L, R));
}
}
|
PHP
<?php
function getKthElement( $n , $k , $L , $R )
{
$l = 1;
$h = $n ;
$total = array ();
$total [0] = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$total [ $i + 1] = $total [ $i ] +
( $R [ $i ] - $L [ $i ]) + 1;
}
$index = -1;
while ( $l <= $h )
{
$m = floor (( $l + $h ) / 2);
if ( $total [ $m ] > $k )
{
$index = $m ;
$h = $m - 1;
}
else if ( $total [ $m ] < $k )
$l = $m + 1;
else
{
$index = $m ;
break ;
}
}
$l = $L [ $index - 1];
$h = $R [ $index - 1];
$x = $k - $total [ $index - 1];
while ( $l <= $h )
{
$m = floor (( $l + $h ) / 2);
if (( $m - $L [ $index - 1]) + 1 == $x )
{
return $m ;
}
else if (( $m - $L [ $index - 1]) + 1 > $x )
$h = $m - 1;
else
$l = $m + 1;
}
}
$L = array ( 1, 8, 21 );
$R = array ( 4, 10, 23 );
$n = count ( $L );
$k = 6;
echo getKthElement( $n , $k , $L , $R );
?>
|
Javascript
<script>
function getKthElement(n,k,L,R)
{
let l = 1;
let h = n;
let total = new Array(n + 1);
total[0] = 0;
for (let i = 0; i < n; i++)
{
total[i + 1] = total[i] +
(R[i] - L[i]) + 1;
}
let index = -1;
while (l <= h)
{
let m = Math.floor((l + h) / 2);
if (total[m] > k)
{
index = m;
h = m - 1;
}
else if (total[m] < k)
l = m + 1;
else
{
index = m;
break ;
}
}
l = L[index - 1];
h = R[index - 1];
let x = k - total[index - 1];
while (l <= h)
{
let m = Math.floor((l + h) / 2);
if ((m - L[index - 1]) + 1 == x)
{
return m;
}
else if ((m - L[index - 1]) + 1 > x)
h = m - 1;
else
l = m + 1;
}
return k;
}
let L = [1, 8, 21 ];
let R = [ 4, 10, 23 ];
let n = L.length;
let k = 6;
document.write(getKthElement(n, k, L, R));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
05 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...