CSES Solutions - Nested Ranges Check
Last Updated :
10 Jul, 2024
Given N ranges, the task is to determine for each range if it contains some other range and if some other range contains it. Range [a,b] contains range [c,d] if a ≤ c and d ≤ b. First print a line that describes for each range (in the input order) if it contains some other range (1) or not (0). Then print a line that describes for each range (in the input order) if some other range contains it (1) or not (0).
Example:
Input: N=4, range[]={{1, 6}, {2, 4}, {4, 8}, {3, 6}}
Output:
1 0 0 0
0 1 0 1
Explanation: The first line of the output (1 0 0 0) indicates whether each range contains some other range or not.
- Range [1, 6] contains [2, 4] and [3, 6], so its corresponding output is 1.
- Ranges [2, 4], [4, 8], and [3, 6] do not contain any other range, so their corresponding outputs are 0.
The second line of the output (0 1 0 1) indicates whether each range is contained by some other range or not.
- Range [1, 6] is not contained by any other range, so its corresponding output is 0.
- Range [2, 4] is contained by [1, 6], so its corresponding output is 1.
- Range [4, 8] is not contained by any other range, so its corresponding output is 0.
- Range [3, 6] is contained by [1, 6], so its corresponding output is 1.
Input: N=4, range[]={{2, 7}, {3, 5}, {1, 8}, {4, 6}}
Output:
1 0 1 0
1 1 0 1
Explanation: The first line of the output (1 0 1 0) indicates whether each range contains some other range or not.
- Range [2, 7] contains [3, 5] and [4, 6], so its corresponding output is 1.
- Range [3, 5] do not contain any other range, so its corresponding output is 0.
- Range [1, 8] contains all other ranges, so its corresponding output is 1.
- Range [4, 6] do not contain any other range, so its corresponding output is 0.
The second line of the output (1 1 0 1) indicates whether each range is contained by some other range or not.
- Range [2, 7] is contained by [1, 8], so its corresponding output is 1.
- Range [3, 5] is contained by [2, 7] and [1, 8], so its corresponding output is 1.
- Range [1, 8] is not contained by any other range, so its corresponding output is 0.
- Range [4, 6] is contained by [2, 7] and [1, 8], so its corresponding output is 1.
Approach: To solve the problem, follow the idea below:
The idea is to sort the ranges in ascending order based on their left end. If two ranges have the same left end, prioritize the one with the larger right end. Now, we only need to check the right end for both operations:
For the “contains” operation:
- Iterate from the right i.e from n-1 to 0. All the ranges before the current range (i.e. from i-1 to n-1) will have a left end greater than the left end of the current range. So, while traversing, if we find any right end with a value greater than the minimum right end so far, we can surely say that our current range contains that range with the minimum value of the right end as our condition is satisfied, current.left ≤ prev.left and prev.min_right ≤ current.right.
For the “contained” operation:
- iterate from the left i.e from 0 to n-1. All the ranges before the current range (i.e. from 0 to i-1) will have a left end lesser than the left end of the current range. So, while traversing, if we find any right end with a value lesser than the maximum right end so far, we can surely say that our current range is contained within that range with the maximum value of the right end as our condition is satisfied, prev.left ≤ current.left and current.right ≤ prev.max_right.
Below is the implementation of above algorithm:
C++
// C++ Code for CSES - Nested Ranges Check
#include <bits/stdc++.h>
using namespace std;
// struct to hold the range information
struct ranges {
// The left and right ends of the range and its index in
// the input order
int l, r, in;
// Overloads the < operator for sorting
bool operator<(const ranges& other) const
{
// If left ends are equal, the range with larger
// right end comes first
if (l == other.l)
return r > other.r;
// Otherwise, the range with smaller left end comes
// first
return l < other.l;
}
};
// Function to determine for each range if it contains some
// other range and if some other range contains it.
vector<vector<int> > checkrange(vector<vector<int> >& r,
int n)
{
vector<ranges> range(n);
vector<int> contains(n), contained(n);
for (int i = 0; i < n; i++) {
range[i].l = r[i][0];
range[i].r = r[i][1];
range[i].in = i;
}
// Sorts the ranges
sort(range.begin(), range.end());
// Checks if a range contains another
int minEnd = 2e9;
for (int i = n - 1; i >= 0; i--) {
// If the right end of the current range is greater
// than minEnd, it contains another
if (range[i].r >= minEnd)
contains[range[i].in] = 1;
// Update minEnd
minEnd = min(minEnd, range[i].r);
}
// Checks if a range is contained by another
int maxEnd = 0;
for (int i = 0; i < n; i++) {
// If the right end of the current range is less
// than maxEnd, it is contained by another
if (range[i].r <= maxEnd)
contained[range[i].in] = 1;
// Update maxEnd
maxEnd = max(maxEnd, range[i].r);
}
// Returns the contains and contained vector
return { contains, contained };
}
// Driver code
int main()
{
int n = 4;
// Example 1
vector<vector<int> > r
= { { 1, 6 }, { 2, 4 }, { 4, 8 }, { 3, 6 } };
vector<vector<int> > res = checkrange(r, n);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++)
cout << res[i][j] << " ";
cout << endl;
}
return 0;
}
Time Complexity: O(N*log(N)), where N is the size of the range.
Auxiliary Space: O(N)
Similar Reads
CSES Solutions â Static Range Sum Queries Given an array arr[] of N integers, your task is to process Q queries of the form: what is the sum of values in range [a,b]? Examples Input: arr[] = {1, 3, 4, 8, 6, 1, 4, 2}, Queries: {{2, 5}, {1, 3}}Output: 21 8Explanation: Query 1: Sum of elements from index 2 to 5 = 3 + 4 + 8 + 6 = 21Query 2: Sum
5 min read
Using Range in C++ Switch Case In C++, we generally know about the switch case which means we give an attribute to the switch case and write down the cases in it so that for each case value we can make desired statements to get executed. We can also define the cases with a range of values instead of a single value. Prerequisites:
2 min read
Check if there exists two non-intersect ranges Given two non-negative integers X and Y, the task is to output the two non-intersecting ranges of non-negative integers such that the sum of the starting and ending point of the first range is equal to X and the product of the starting and ending point of the second range is equal Y. If there is no
7 min read
Class 11 NCERT Solutions - Chapter 6 Linear Inequalities - Exercise 6.3 Solve the following system of inequalities graphically:Question 1: x ⥠3, y ⥠2 Solution: For equation 1: Now draw a solid line x = 3 in the graph (because (x = 3) is the part of the given equation) we need at least two solutions of the equation. So, we can use the following table to draw the graph:
15+ min read
Class 11 NCERT Solutions- Chapter 6 Linear Inequalities - Exercise 6.1 | Set 1 Question 1. Solve 24x < 100, when (i) x is a natural number. (ii) x is an integer. Solution: (i) when x is a natural number. Clearly x>0 because from definition (N =1,2,3,4,5,6.....)Now we have to divide the inequation by 24 we get x<25/6But x is a natural number that is the solution will b
8 min read