# Queries to check if a number lies in N ranges of L-R

Given N ranges and Q queries consisting of numbers. Every range consists of L and R. The task is to check if the given number lies in any of the given ranges or not for every query.

**Note:** There is no overlapping range.

**Examples:**

Input:range[] = { {5, 6}, {1, 3}, {8, 10}

Q = 4

1st query: 2

2nd query: 3

3rd query: 4

4th query: 7

Output:

Yes

Yes

No

No1st query: 2 lies in a range 1-3

2nd query: 3 lies in a range 1-3

3rd query: 4 does not lie in any of the given range.

4th query: 7 does not lie in any of the given range.

**Approach:** Below is the step by step algorithm to solve this problem:

- Hash the L of every range as 1, and hash the R of every range as 2.
- Push the L and R separately into a container.
- Sort the elements in the container, all the range L and R will be adjacent to each other as do not overlap.
- For every query, use binary search to find the first occurrence of a number same or greater than it. This can be done using the lower_bound function.
- If there is any value which is equal to the query, then the number overlaps the range.
- If there is no value which is equal to the query, then check if the greater element is hashed as 1 or 2.
- If it is hashed as 1, then the number does not overlaps, else it does overlap.

**Below is the implementaion of above approach:**

`// C++ program to check if the ` `// number lies in given range ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function that answers every query ` `void` `answerQueries(` `int` `a[][2], ` `int` `n, ` `int` `queries[], ` `int` `q) ` `{ ` ` ` ` ` `// container to store all range ` ` ` `vector<` `int` `> v; ` ` ` ` ` `// hash the L and R ` ` ` `unordered_map<` `int` `, ` `int` `> mpp; ` ` ` ` ` `// Push the element to container ` ` ` `// and hash the L and R ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `v.push_back(a[i][0]); ` ` ` `mpp[a[i][0]] = 1; ` ` ` `v.push_back(a[i][1]); ` ` ` `mpp[a[i][1]] = 2; ` ` ` `} ` ` ` ` ` `// sort the elements in container ` ` ` `sort(v.begin(), v.end()); ` ` ` `for` `(` `int` `i = 0; i < q; i++) { ` ` ` ` ` `// each query ` ` ` `int` `num = queries[i]; ` ` ` ` ` `// get the number same or greater than integer ` ` ` `int` `ind = lower_bound(v.begin(), v.end(), num) - v.begin(); ` ` ` ` ` `// if it lies ` ` ` `if` `(v[ind] == num) { ` ` ` `cout << ` `"Yes\n"` `; ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// check if greater is hashed as 2 ` ` ` `if` `(mpp[v[ind]] == 2) ` ` ` `cout << ` `"Yes\n"` `; ` ` ` ` ` `else` `// check if greater is hashed as 1 ` ` ` `cout << ` `"No\n"` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[][2] = { { 5, 6 }, { 1, 3 }, { 8, 10 } }; ` ` ` ` ` `int` `n = 3; ` ` ` ` ` `int` `queries[] = { 2, 3, 4, 7 }; ` ` ` ` ` `int` `q = ` `sizeof` `(queries) / ` `sizeof` `(queries[0]); ` ` ` ` ` `// function call to answer queries ` ` ` `answerQueries(a, n, queries, q); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes Yes No No

## Recommended Posts:

- Queries on probability of even or odd number in given ranges
- Number of intersections between two ranges
- Queries to check whether a given digit is present in the given Range
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Count number of indices such that s[i] = s[i+1] : Range queries
- Range Queries to Find number of sub-arrays with a given xor
- Queries to answer the number of ones and zero to the left of given index
- Queries for number of distinct elements in a subarray
- Queries for number of distinct elements in a subarray | Set 2
- Number of comparisons in each direction for m queries in linear search
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Compress the array into Ranges
- Count Primes in Ranges
- Products of ranges in an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.