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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live