# Find if there exists a direction for ranges such that no two range intersect

Given **N** ranges **[L, R]** with velocities **vel[]**. The task is to assign each range a direction i.e. either **left** or **right**. All the ranges will start moving in the assigned direction at time **t = 0**. Find, if there is an assignment of directions possible given that no two ranges overlap at infinite time.

**Examples:**

Input:range[][] = {{1, 2}, {2, 5}, {3, 10}, {4, 4}, {5, 7}},

vel[] = {3, 1, 1, 1, 10}

Output:No

Intervals {2, 5}, {3, 10} and {4, 4} share a common point 4

and have the same velocity.

Input:range[][] = {{1, 2}, {2, 5}, {3, 10}, {4, 4}, {5, 7}},

vel[] = {3, 1, 11, 1, 10}

Output:Yes

**Approach:**

- At infinite time, if there are two ranges with different velocities then they can never overlap irrespective of their directions since range with greater velocity will always be ahead of that with slower velocity.
- Now, consider ranges with same velocity. If there exist a point lying in at least 3 ranges with same velocity then the assignment of directions is not possible because even if two of these ranges are assigned a different direction but the third range will definitely intersect with the any one of the other ranges with the same direction.
- So, for all the velocities, find whether there exists at least 3 ranges such that they share a common point, if yes then assignment is not possible. Otherwise it is possible.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define MAX 100001 ` `using` `namespace` `std; ` ` ` `// Structure to hold details of ` `// each interval ` `typedef` `struct` `{ ` ` ` `int` `l, r, v; ` `} interval; ` ` ` `// Comparator to sort intervals ` `// based on velocity ` `bool` `cmp(interval a, interval b) ` `{ ` ` ` `return` `a.v < b.v; ` `} ` ` ` `// Function that returns true if the ` `// assignment of directions is possible ` `bool` `isPossible(` `int` `range[][3], ` `int` `N) ` `{ ` ` ` `interval test[N]; ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `test[i].l = range[i][0]; ` ` ` `test[i].r = range[i][1]; ` ` ` `test[i].v = range[i][2]; ` ` ` `} ` ` ` ` ` `// Sort the intervals based on velocity ` ` ` `sort(test, test + N, cmp); ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `int` `count[MAX] = { 0 }; ` ` ` `int` `current_velocity = test[i].v; ` ` ` ` ` `int` `j = i; ` ` ` ` ` `// Test the condition for all intervals ` ` ` `// with same velocity ` ` ` `while` `(j < N && test[j].v == current_velocity) { ` ` ` `for` `(` `int` `k = test[j].l; k <= test[j].r; k++) { ` ` ` `count[k]++; ` ` ` ` ` `// If for any velocity, 3 or more intervals ` ` ` `// share a common point return false ` ` ` `if` `(count[k] >= 3) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `j++; ` ` ` `} ` ` ` ` ` `i = j - 1; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `range[][3] = { { 1, 2, 3 }, ` ` ` `{ 2, 5, 1 }, ` ` ` `{ 3, 10, 1 }, ` ` ` `{ 4, 4, 1 }, ` ` ` `{ 5, 7, 10 } }; ` ` ` `int` `n = ` `sizeof` `(range) / ` `sizeof` `(range[0]); ` ` ` ` ` `if` `(isPossible(range, n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

No

## Recommended Posts:

- Find a range that covers all the elements of given N ranges
- Find the missing elements from 1 to M in given N ranges
- Find the missing elements from 1 to M in given N ranges | Set-2
- Find k-th smallest element in given n ranges
- Find the kth element in the series generated by the given N ranges
- Pair with given product | Set 1 (Find if any pair exists)
- Find XOR of numbers from the range [L, R]
- Find the one missing number in range
- Find missing elements of a range
- Find array elements with frequencies in range [l , r]
- Queries to update a given index and find gcd in range
- Find smallest range containing elements from k lists
- Range Queries to Find number of sub-arrays with a given xor
- Find element at given index after given range reversals
- Print direction of moves such that you stay within the [-k, +k] boundary

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.