Queries for greater than and not less than
Given an array of N integers. There will be Q queries, each include two integer of form q and x, 0 <= q <= 1. Queries are of two types:
- In first query (q = 0), the task is to find count of integers which are not less than x (OR greater than or equal to x).
- In second query (q = 1), the task is to find count of integers greater than x.
Examples:
Input : arr[] = { 1, 2, 3, 4 } and Q = 3
Query 1: 0 5
Query 2: 1 3
Query 3: 0 3
Output :0
1
2
Explanation:
x = 5, q = 0 : There are no elements greater than or equal to it.
x = 3, q = 1 : There is one element greater than 3 which is 4.
x = 3, q = 0 : There are two elements greater than or equal to 3.
Method 1: A Naive approach can be for each query, traverse the whole array and count integers less or greater than x, depending on q. Time Complexity for this approach will be O(Q*N).
Method 2: An efficient approach can be sort the array and use binary search for each query. This will take O(NlogN + QlogN).
Below is the implementation of this approach :
C++
#include<bits/stdc++.h>
using namespace std;
int lower_bound( int arr[], int start, int end, int x)
{
while (start < end)
{
int mid = (start + end)>>1;
if (arr[mid] >= x)
end = mid;
else
start = mid + 1;
}
return start;
}
int upper_bound( int arr[], int start, int end, int x)
{
while (start < end)
{
int mid = (start + end)>>1;
if (arr[mid] <= x)
start = mid + 1;
else
end = mid;
}
return start;
}
void query( int arr[], int n, int type, int x)
{
if (type)
cout << n - upper_bound(arr, 0, n, x) << endl;
else
cout << n - lower_bound(arr, 0, n, x) << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof (arr)/ sizeof (arr[0]);
sort(arr, arr + n);
query(arr, n, 0, 5);
query(arr, n, 1, 3);
query(arr, n, 0, 3);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int lower_bound( int arr[], int start,
int end, int x)
{
while (start < end)
{
int mid = (start + end)>> 1 ;
if (arr[mid] >= x)
end = mid;
else
start = mid + 1 ;
}
return start;
}
static int upper_bound( int arr[], int start, int end, int x)
{
while (start < end)
{
int mid = (start + end)>> 1 ;
if (arr[mid] <= x)
start = mid + 1 ;
else
end = mid;
}
return start;
}
static void query( int arr[], int n, int type, int x)
{
if (type== 1 )
System.out.println(n - upper_bound(arr, 0 , n, x));
else
System.out.println(n - lower_bound(arr, 0 , n, x));
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int n = arr.length;
Arrays.sort(arr);
query(arr, n, 0 , 5 );
query(arr, n, 1 , 3 );
query(arr, n, 0 , 3 );
}
}
|
Python3
def lower_bound(arr, start, end, x):
while (start < end):
mid = (start + end) >> 1
if (arr[mid] > = x):
end = mid
else :
start = mid + 1
return start
def upper_bound(arr, start, end, x):
while (start < end):
mid = (start + end) >> 1
if (arr[mid] < = x):
start = mid + 1
else :
end = mid
return start
def query(arr, n, type , x):
if ( type = = 1 ):
print (n - upper_bound(arr, 0 , n, x))
else :
print (n - lower_bound(arr, 0 , n, x))
arr = [ 1 , 2 , 3 , 4 ]
n = len (arr)
arr.sort()
query(arr, n, 0 , 5 )
query(arr, n, 1 , 3 )
query(arr, n, 0 , 3 )
|
C#
using System;
class GFG {
static int lower_bound( int []arr, int start,
int end, int x)
{
while (start < end)
{
int mid = (start + end)>>1;
if (arr[mid] >= x)
end = mid;
else
start = mid + 1;
}
return start;
}
static int upper_bound( int []arr, int start,
int end, int x)
{
while (start < end)
{
int mid = (start + end)>>1;
if (arr[mid] <= x)
start = mid + 1;
else
end = mid;
}
return start;
}
static void query( int []arr, int n, int type, int x)
{
if (type==1)
Console.WriteLine(n - upper_bound(arr, 0, n, x));
else
Console.WriteLine(n - lower_bound(arr, 0, n, x));
}
public static void Main ()
{
int []arr = {1, 2, 3, 4};
int n = arr.Length;
Array.Sort(arr);
query(arr, n, 0, 5);
query(arr, n, 1, 3);
query(arr, n, 0, 3);
}
}
|
PHP
<?php
function lower_bound( $arr , $start , $end , $x )
{
while ( $start < $end )
{
$mid = ( $start + $end ) >> 1;
if ( $arr [ $mid ] >= $x )
$end = $mid ;
else
$start = $mid + 1;
}
return $start ;
}
function upper_bound( $arr , $start , $end , $x )
{
while ( $start < $end )
{
$mid = ( $start + $end ) >> 1;
if ( $arr [ $mid ] <= $x )
$start = $mid + 1;
else
$end = $mid ;
}
return $start ;
}
function query( $arr , $n , $type , $x )
{
if ( $type )
echo $n - upper_bound( $arr , 0, $n , $x ) , "\n" ;
else
echo $n - lower_bound( $arr , 0, $n , $x ) , "\n" ;
}
$arr = array (1, 2, 3, 4);
$n = count ( $arr );
sort( $arr );
query( $arr , $n , 0, 5);
query( $arr , $n , 1, 3);
query( $arr , $n , 0, 3);
?>
|
Javascript
<script>
function lower_bound(arr, start,
end, x)
{
while (start < end)
{
let mid = (start + end)>>1;
if (arr[mid] >= x)
end = mid;
else
start = mid + 1;
}
return start;
}
function upper_bound(arr, start, end, x)
{
while (start < end)
{
let mid = (start + end)>>1;
if (arr[mid] <= x)
start = mid + 1;
else
end = mid;
}
return start;
}
function query(arr, n, type, x)
{
if (type==1)
document.write(n - upper_bound(arr, 0, n, x) + "<br/>" );
else
document.write(n - lower_bound(arr, 0, n, x) + "<br/>" );
}
let arr = [ 1, 2, 3, 4 ];
let n = arr.length;
arr.sort();
query(arr, n, 0, 5);
query(arr, n, 1, 3);
query(arr, n, 0, 3);
</script>
|
Output:
0
1
2
Time Complexity : O( (N + Q) * logN).
Auxiliary Space : O(1)
Last Updated :
11 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...