- Related Questions & Answers
- Check if it is possible to reach a number by making jumps of two given length in Python
- Check if the given number K is enough to reach the end of an array in Python
- Find if it is possible to get a ratio from given ranges of costs and quantities in C++
- Check if it is possible to return to the starting position after moving in the given directions in C++
- Find minimum steps required to reach the end of a matrix in C++
- Check if it is possible to reach vector B by rotating vector A and adding vector C to its in Python
- Find if it is possible to get a ratio from given ranges of costs and quantities in Python
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to draw a straight line with the given direction cosines in Python
- Check if it is possible to form string B from A under the given constraint in Python
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Check if it is possible to convert one string into another with given constraints in Python
- C Program for Minimum number of jumps to reach the end

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have n points on x-axis and the list of allowed translation between the points. Find if it is possible to reach the end from starting point through these transactions only. So if there is a translation between points x1 and x2, then we can move from point x to any intermediate points between x1 and x2, or directly to x2. So if n = 5. And transactions are 0 to 2, 2 to 4, and 3 to 5. Then output will be YES. There is a path from 0→2→3→5.

We have to sort the list according to the first element of the pairs. Then start from second pair of the list and check if the first element of the pair is in between second element of previous pair and the 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 we will check whether the point we have reached is the destination point and the point from which we have started is start point. If so, display YES otherwise display NO.

#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; bool isPathPairFound(int n, vector<pair<int, int> > array) { sort(array.begin(),array.end()); int start_point = array[0].first; int end_point=array[0].second; for (int i=1; i<n; i++) { if (array[i].first > end_point) break; end_point=max(end_point,array[i].second); } return (n <= end_point && start_point==0); } int main() { vector<pair<int, int> > array; array.push_back(make_pair(0,2)); array.push_back(make_pair(2,4)); array.push_back(make_pair(3,5)); if (isPathPairFound(5,array)) cout << "Path has found"; else cout << "NO Path has found"; }

Path has found

Advertisements