# Find if it is possible to reach the end through given transitions

Given, n points on X-axis and the list of allowed transition between the points. Find if it is possible to reach the end from starting point through these transitions only.

**Note**: If there is a transition between points x1 and x2, then you can move from point x to any intermediate points between x1 and x2 or directly to x2.

Examples:

Input : n = 5 , Transitions allowed: 0 -> 2 2 -> 4 3 -> 5 Output : YES Explanation : We can move from 0 to 5 using the allowed transitions. 0->2->3->5 Input : n = 7 , Transitions allowed: 0 -> 4 2 -> 5 6 -> 7 Output : NO Explanation : We can't move from 0 to 7 as there is no transition between 5 and 6.

The idea to solve this problem is to first sort this list according to first element of the pairs. Then start traversing from the second pair of the list and check if the first element of this pair is in between second element of previous pair and second element of current pair or not. This condition is used to check if there is a path between two consecutive pairs.

At the end check if the point we have reached is the destination point and the point from which we have started is start point. If so, print YES otherwise print NO.

`// C++ implementation of above idea ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check if it is possible to ` `// reach the end through given points ` `bool` `checkPathPairs(` `int` `n, vector<pair<` `int` `, ` `int` `> > vec) ` `{ ` ` ` `// sort the list of pairs ` ` ` `// according to first element ` ` ` `sort(vec.begin(),vec.end()); ` ` ` ` ` `int` `start = vec[0].first; ` ` ` ` ` `int` `end=vec[0].second; ` ` ` ` ` `// start traversing from 2nd pair ` ` ` `for` `(` `int` `i=1; i<n; i++) ` ` ` `{ ` ` ` `// check if first element of current pair ` ` ` `// is in between second element of previous ` ` ` `// and current pair ` ` ` `if` `(vec[i].first > end) ` ` ` `break` `; ` ` ` ` ` `end=max(end,vec[i].second); ` ` ` `} ` ` ` ` ` `return` `(n <= end && start==0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<pair<` `int` `, ` `int` `> > vec; ` ` ` `vec.push_back(make_pair(0,4)); ` ` ` `vec.push_back(make_pair(2,5)); ` ` ` `vec.push_back(make_pair(6,7)); ` ` ` ` ` `if` `(checkPathPairs(7,vec)) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

NO

## Recommended Posts:

- Quick ways to check for Prime and find next Prime in Java
- Find two numbers from their sum and XOR
- Find the arrangement of queue at given time
- Find the position of the last removed element from the array
- Find if neat arrangement of cups and shelves can be made
- Find the minimum time after which one can exchange notes
- Minimum total cost incurred to reach the last station
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Queries to find distance between two nodes of a Binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Find the number of operations required to make all array elements Equal
- Find Largest Special Prime which is less than or equal to a given number
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Find (a^b)%m where 'b' is very large

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.