Find if it is possible to reach the end through given transitions
Last Updated :
29 Mar, 2023
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++
// 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;
}
Python3
# Python3 implementation of above idea
# function to check if it is possible to
# reach the end through given points
def checkPathPairs(n: int, vec: list) -> bool:
# sort the list of pairs
# according to first element
vec.sort(key = lambda a: a[0])
start = vec[0][0]
end = vec[0][1]
# start traversing from 2nd pair
for i in range(1, n):
# check if first element of
# current pair is in between
# second element of previous
# and current pair
if vec[i][1] > end:
break
end = max(end, vec[i][1])
return (n <= end and start == 0)
# Driver Code
if __name__ == "__main__":
vec = []
vec.append((0, 4))
vec.append((2, 5))
vec.append((6, 7))
if checkPathPairs(7, vec):
print("YES")
else:
print("NO")
# This code is contributed by
# sanjeev2552
C#
// C# implementation of above idea
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
// function to check if it is possible to
// reach the end through given points
public static bool CheckPathPairs(int n, List<(int, int)> vec)
{
// sort the list of pairs
// according to first element
vec = vec.OrderBy(p => p.Item1).ToList();
int start = vec[0].Item1;
int end = vec[0].Item2;
// 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].Item1 > end)
{
break;
}
end = Math.Max(end, vec[i].Item2);
}
return (n <= end && start == 0);
}
// Driver code
public static void Main(string[] args)
{
List<(int, int)> vec = new List<(int, int)>();
vec.Add((0, 4));
vec.Add((2, 5));
vec.Add((6, 7));
if (CheckPathPairs(7, vec))
{
Console.WriteLine("YES");
}
else
{
Console.WriteLine("NO");
}
}
}
JavaScript
<script>
// JavaScript program to implement above approach
// function to check if it is possible to
// reach the end through given points
function checkPathPairs(n,vec){
// sort the list of pairs
// according to first element
vec.sort((a,b)=>a-b)
let start = vec[0][0]
let end = vec[0][1]
// start traversing from 2nd pair
for(let 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][1] > end)
break
end = Math.max(end, vec[i][1])
}
return (n <= end && start == 0)
}
// driver program
let vec = []
vec.push([0, 4])
vec.push([2, 5])
vec.push([6, 7])
if(checkPathPairs(7, vec))
document.write("YES")
else
document.write("NO")
// This code is contributed by shinjanpatra
</script>
Java
import java.util.Arrays;
class Main {
// function to check if it is possible to reach the end through given points
public static boolean checkPathPairs(int n, int[][] vec) {
// sort the list of pairs according to first element
Arrays.sort(vec, (a, b) -> Integer.compare(a[0], b[0]));
int start = vec[0][0];
int end = vec[0][1];
// 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][1] > end)
break;
end = Math.max(end, vec[i][1]);
}
return (n <= end && start == 0);
}
public static void main(String[] args) {
int[][] vec = new int[][] { { 0, 4 }, { 2, 5 }, { 6, 7 } };
int n = 7;
if (checkPathPairs(n, vec))
System.out.println("YES");
else
System.out.println("NO");
}
}
Time Complexity: O(n log n)
Auxiliary Space: O(1)
Similar Reads
Check if it is possible to reach (X, Y) from (1, 0) by given steps Given two positive integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 0) by the given steps. In each step, possible moves from any cell (a, b) are (a, b + a) or (a + b, b). Print "Yes" if possible. Otherwise, print "No". Examples: Input: X = 2, Y = 7Output: YesExplanat
5 min read
Find the time when last car reaches end of track in given race Given a car race track of length N units. where each car moves with a speed of 1 unit per second. Given two arrays left[] and right[] denoting the positions of the cars moving towards left and right respectively. When two cars running in two opposite directions meet at some point on the track, they
5 min read
Check if it is possible to move from (a, 0) to (b, 0) with given jumps Given two points, i.e. (a, 0) to (b, 0). The task is to check whether it is possible to move from (a,0) to (b,0) or not. One can move as (a, 0), (a+x, 0), (a+x+1, 0), (a, 2*x, 0), (a, 2*x+1, 0)...... Examples: Input: a = 3, x = 10, b = 4Output: No Input: a = 3, x = 2, b = 5Output: Yes Approach: An a
5 min read
Check if it is possible to return to the starting position after moving in the given directions Given a string S having N directions in which a person travels. The task is to check if he/she will be able to return to the same place where he/she started. On Day i(1 <= i <= N), he will travel a positive distance in the following direction: North if the i-th letter of str is N West if the i
8 min read
POTD Solutions | 4 Novâ 23 | Find Transition Point View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Search algorithm but will also help you build up
4 min read