- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given a 2D array arr[][] containing intervals and a number ‘value’. The goal is to find the number of intervals present in arr between which value lies. For example intervals are [ [1,5], [3,7] ] and value=4 then it lies in both these intervals and count would be 2.

**For Example**

arr[4][2] = { { 1, 20 }, { 12, 25 }, { 32, 40 }, { 15, 18 } } value=16

Count of number of intervals in which a given value lies are: 3

The value 16 lies between 1−20, 12−25 and 15−18

arr[4][2] = {{ 1, 20 }, { 20,30 }, { 30, 40 }, { 40, 50 }} value=60

Count of number of intervals in which a given value lies are: 0

The value 60 is larger than all maximum ranges of intervals present in arr[][].

**Approach used in the below program is as follows** −

In this approach we will generate a frequency array arr_2[] for all numbers of the ranges present in arr. So for each range's arr[i][0] and arr[i][1] the frequency will be incremented in arr_2[ arr[i][0 or 1] ]. At the end we will update frequency array with arr_2[i]=arr_2[i]+arr_2[i−1] as numbers less than i-1 will also be less than i so frequency will be added. In this way we will get arr_2[value] as all ranges to which value lies.

Take an integer array arr[][] containing ranges.

Take an integer value as input.

Function intervals_values(int arr[][2], int size, int value) takes arr and value and returns a count of the number of intervals in which a given value lies.

Take frequency array arr_2[].

Take low and highest as INT_MAX and INT_MIN.

Traverse arr[][] using for loop from i=0 to i<size.

Take temp as left of range and increment its frequency in arr_2[temp]

Take temp_2 as right of range and increment its frequency in arr_2[temp_2+1]

If temp<low set low=temp and if temp_2>highest set highest as temp_2.

Traverse frequency array and update it arr_2[i]=arr_2[i]+arr_2[i+1].

At the end return arr_2[value] as result.

#include<bits/stdc++.h> using namespace std; #define max 1000 int intervals_values(int arr[][2], int size, int value){ int arr_2[max]; int low = INT_MAX; int highest = INT_MIN; for(int i = 0; i < size; i++){ int temp = arr[i][0]; arr_2[temp] = arr_2[temp] + 1; int temp_2 = arr[i][1]; arr_2[temp_2 + 1] = arr_2[temp_2 + 1] − 1; if(temp < low){ low = temp; } if(temp_2 > highest){ highest = temp_2; } } for (int i = low; i <= highest; i++){ arr_2[i] = arr_2[i] + arr_2[i − 1]; } return arr_2[value]; } int main(){ int arr[4][2] = { { 3, 20 }, { 2, 13 }, { 25, 30 }, { 15, 40 } }; int size = sizeof(arr) / sizeof(arr[0]); int value = 28; cout<<"Count the number of intervals in which a given value lies are: "<<intervals_values(arr, size, value); return 0; }

If we run the above code it will generate the following output −

Count the number of intervals in which a given value lies are: 18830628

- Related Questions & Answers
- Program to count number of intervals which are intersecting at given point in Python
- Count the number of rectangles such that ratio of sides lies in the range [a,b] in C++.
- Count digits in given number N which divide N in C++
- Find number from given list for which value of the function is closest to A in C++
- Check if any two intervals overlap among a given set of intervals in C++
- Count index pairs which satisfy the given condition in C++
- Count the number of common divisors of the given strings in C++
- Count the number of operations required to reduce the given number in C++
- Program to count number of intervals that is totally contained inside other intervals in Python
- Count the number of key/value pairs in HybridDictionary in C#
- Count the number of key/value pairs in the Hashtable in C#
- Count the number of rhombi possible inside a rectangle of given size in C++
- Count occurrences of the average of array elements with a given number in C++
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added in C++
- Count number of subsets having a particular XOR value in C++

Advertisements