# Queries for greater than and not less than using C++

C++Server Side ProgrammingProgramming

In this article, we are given a problem, we are given an array, and there are two types of queries we need to answer.

• Type 0 − we have to calculate the number of greater elements than or equal to x(given value).
• Type 1 − we have to calculate the number of strictly greater elements than x(given value).

So here is a simple example −

Input : arr[] = { 10, 15, 30 , 40, 45 } and Q = 3
Query 1: 0 50
Query 2: 1 40
Query 3: 0 30
Output :
0
1
3
Explanation:
x = 50, q = 0 : No elements greater than or equal to 50.
x = 40, q = 1 : 45 is greater than 40.
x = 30, q = 0 : three elements 30, 40, 45 are greater than or equal to 30.

## Approach to find The Solution

We can use two different methods to find the solution. Firstly we will use the brute force solution and then check if it can work for higher constraints or not. If not, then we proceed to optimize our solution.

## Brute Force Approach

In this approach, we will traverse through the array for all q queries and find the numbers that satisfy the given conditions.

## Example

#include <bits/stdc++.h>
using namespace std;
void query(int *arr, int n, int type, int val) {
int count = 0; // answer
if(!type) { // when type 0 query is asked
for(int i = 0; i < n; i++) {
if(arr[i] >= val)
count++;
}
} else { // when type 1 query is asked
for(int i = 0; i < n; i++) {
if(arr[i] > val)
count++;
}
}
cout << count << "\n";
}
int main() {
int ARR[] = { 10, 15, 30, 40, 45 };
int n = sizeof(ARR)/sizeof(ARR[0]); // size of our array
query(ARR, n, 0, 50); // query 1
query(ARR, n, 1, 40); // query 2
query(ARR, n, 0, 30); // query 3
return 0;
}

## Output

0
1
3

In the above approach, we are simply traversing through the array and calculating the answer for the queries; this approach is working for the given examples, but if we encounter higher constraint, this approach will fail as the overall time complexity of the program is O(N*Q) where N is the size of our array and Q is the number of queries so now we will optimize this approach such that it works for higher constraints as well.

## Efficient Approach

In this approach, we will be using binary search to find the upper bound and lower bound of the given value. We first sort our array using binary search and then apply our lower bound and upper bound functions accordingly.

## Example

#include <bits/stdc++.h>

using namespace std;
void lowerbound(int *arr, int n, int val) {
int l = -1, r = n;
while(r - l > 1) { // binary searching the answer
int mid = (l+r)/2;
if(arr[mid] >= val)
r = mid;
else
l = mid;
}
if(r == n) // if r is unmoved then it means there is no element that satisfy the condition
cout << "0\n";
else
cout << n - r << "\n";
}
void upperbound(int *arr, int n, int val) {
int l = -1, r = n;
while(r - l > 1) { // binary searching the answer
int mid = (l+r)/2;
if(arr[mid] > val)
r = mid;
else
l = mid;
}
if(r == n)// if r is unmoved then it means there is no element that satisfy the condition
cout << "0\n";
else
cout << n - r <<"\n";
}
void query(int *arr, int n, int type, int val) {
if(!type) // if type == 0 we call lower bound function
lowerbound(arr, n, val);
else // if type == 1 we call upperbound function
upperbound(arr, n, val);
}
int main() {
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr)/sizeof(arr[0]); // size of our array
sort(arr, arr+n); // sorting the array
query(arr, n, 0, 5); // query 1
query(arr, n, 1, 3); // query 2
query(arr, n, 0, 3); // query 3
return 0;
}

## Output

0
1
2

The above code works on a binary search that decreases our time complexity substantially. Thus our final complexity becomes O(NlogN), where N is the size of our array.

## Explanation of the above code

In this approach, we will be using binary search to find the upper bound and lower bound of the given value. Now for binary search, we first sort our array as it works only with sorted arrays. We make our lower bound and upper bound function that helps us find the first number that satisfies the type 0, type 1 conditions, respectively, now as we have sorted the array. We found the first number that helps the condition, so the elements after this element also follow the condition, so we print the difference of the index of this element with N(the size of our array).

## Conclusion

In this article, we solve a problem to solve Queries for greater than and not less than using Binary search. We also learned the C++ program for this problem and the complete approach ( Normal and efficient ) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.

Published on 26-Nov-2021 09:36:14
Advertisements