Search an element in given N ranges
Given an array of N sorted ranges and a number K. The task is to find the index of the range in which K lies. If K does not lie in any of the given ranges then print -1.
Note: None of the given ranges coincide.
Examples:
Input: arr[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } }, K = 6
Output: 1
6 lies in the range {4, 7} with index = 1
Input: arr[] = { { 1, 3 }, { 4, 7 }, { 9, 11 } }, K = 8
Output: -1
Naive approach: The following steps can be followed to solve the above problem.
- Traverse all the ranges.
- Check if the condition K >= arr[i].first && K <= arr[i].second holds in any of the iterations.
- If the number K does not lie in any of the given ranges then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber(pair< int , int > a[], int n, int K)
{
for ( int i = 0; i < n; i++) {
if (K >= a[i].first && K <= a[i].second)
return i;
}
return -1;
}
int main()
{
pair< int , int > a[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } };
int n = sizeof (a) / sizeof (a[0]);
int k = 6;
int index = findNumber(a, n, k);
if (index != -1)
cout << index;
else
cout << -1;
return 0;
}
|
Java
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int findNumber(pair a[],
int n, int K)
{
for ( int i = 0 ; i < n; i++)
{
if (K >= a[i].first &&
K <= a[i].second)
return i;
}
return - 1 ;
}
public static void main(String[] args)
{
pair a[] = { new pair( 1 , 3 ),
new pair( 4 , 7 ),
new pair( 8 , 11 )};
int n = a.length;
int k = 6 ;
int index = findNumber(a, n, k);
if (index != - 1 )
System.out.println(index);
else
System.out.println(- 1 );
}
}
|
Python3
def findNumber(a, n, K):
for i in range ( 0 , n, 1 ):
if (K > = a[i][ 0 ] and K < = a[i][ 1 ]):
return i
return - 1
if __name__ = = '__main__' :
a = [[ 1 , 3 ], [ 4 , 7 ], [ 8 , 11 ]]
n = len (a)
k = 6
index = findNumber(a, n, k)
if (index ! = - 1 ):
print (index, end = "")
else :
print ( - 1 , end = "")
|
C#
using System;
class GFG
{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int findNumber(pair []a,
int n, int K)
{
for ( int i = 0; i < n; i++)
{
if (K >= a[i].first &&
K <= a[i].second)
return i;
}
return -1;
}
public static void Main(String[] args)
{
pair []a = { new pair(1, 3 ),
new pair(4, 7 ),
new pair(8, 11 )};
int n = a.Length;
int k = 6;
int index = findNumber(a, n, k);
if (index != -1)
Console.WriteLine(index);
else
Console.WriteLine(-1);
}
}
|
Javascript
<script>
function findNumber(a, n, K)
{
for ( var i = 0; i < n; i++) {
if (K >= a[i][0] && K <= a[i][1])
return i;
}
return -1;
}
var a = [ [ 1, 3 ], [ 4, 7 ], [ 8, 11 ] ];
var n = a.length;
var k = 6;
var index = findNumber(a, n, k);
if (index != -1)
document.write( index);
else
document.write( -1);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times to check if the number lies in the given range. Where N is the number of pairs in the array.
Auxiliary Space: O(1), as we are not using any extra space.
Efficient Approach: Binary Search can be used to find the element in O(log N).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber(pair< int , int > a[], int n, int K)
{
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) >> 1;
if (K >= a[mid].first && K <= a[mid].second)
return mid;
else if (K < a[mid].first)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
int main()
{
pair< int , int > a[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } };
int n = sizeof (a) / sizeof (a[0]);
int k = 6;
int index = findNumber(a, n, k);
if (index != -1)
cout << index;
else
cout << -1;
return 0;
}
|
Java
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int findNumber(pair a[], int n, int K)
{
int low = 0 , high = n - 1 ;
while (low <= high)
{
int mid = (low + high) >> 1 ;
if (K >= a[mid].first &&
K <= a[mid].second)
return mid;
else if (K < a[mid].first)
high = mid - 1 ;
else
low = mid + 1 ;
}
return - 1 ;
}
public static void main(String[] args)
{
pair a[] = { new pair( 1 , 3 ),
new pair( 4 , 7 ),
new pair( 8 , 11 ) };
int n = a.length;
int k = 6 ;
int index = findNumber(a, n, k);
if (index != - 1 )
System.out.println(index);
else
System.out.println(- 1 );
}
}
|
Python3
def findNumber(a, n, K):
low = 0
high = n - 1
while (low < = high):
mid = (low + high) >> 1
if (K > = a[mid][ 0 ] and K < = a[mid][ 1 ]):
return mid
elif (K < a[mid][ 0 ]):
high = mid - 1
else :
low = mid + 1
return - 1
a = [ [ 1 , 3 ], [ 4 , 7 ], [ 8 , 11 ] ]
n = len (a)
k = 6
index = findNumber(a, n, k)
if (index ! = - 1 ):
print (index)
else :
print ( - 1 )
|
C#
using System;
class GFG
{
public class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int findNumber(pair []a, int n, int K)
{
int low = 0, high = n - 1;
while (low <= high)
{
int mid = (low + high) >> 1;
if (K >= a[mid].first &&
K <= a[mid].second)
return mid;
else if (K < a[mid].first)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
public static void Main(String[] args)
{
pair []a = { new pair(1, 3),
new pair(4, 7),
new pair(8, 11)};
int n = a.Length;
int k = 6;
int index = findNumber(a, n, k);
if (index != -1)
Console.WriteLine(index);
else
Console.WriteLine(-1);
}
}
|
Javascript
<script>
class pair {
constructor(first , second) {
this .first = first;
this .second = second;
}
}
function findNumber( a , n , K) {
var low = 0, high = n - 1;
while (low <= high) {
var mid = (low + high) >> 1;
if (K >= a[mid].first && K <= a[mid].second)
return mid;
else if (K < a[mid].first)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
var a = [ new pair(1, 3), new pair(4, 7),
new pair(8, 11) ];
var n = a.length;
var k = 6;
var index = findNumber(a, n, k);
if (index != -1)
document.write(index);
else
document.write(-1);
</script>
|
Time Complexity: O(log N), as we are using binary search, in each traversal we divide the array into two halves and choose one of them to search further in. So the effective time is 1+1/2+1/4+….+1/2^N which is equivalent to logN. Where N is the number of pairs in the array.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
18 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...