![]() |
VOOZH | about |
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).
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:
1 0 0 0 0 1 0 1
Time Complexity: O(N*log(N)), where N is the size of the range.
Auxiliary Space: O(N)