Company: Sap_20nov

Difficulty: medium

Problem Statement

Path Through All Vertices in Increasing Order Problem Description You are given an undirected graph with N vertices, labeled 1 through N, connected by M edges. The graph is specified by two arrays, A and B, each of length M, where the pair (A[K], B[K]) for K from 0 to M-1 describes an edge joining vertex A[K] and vertex B[K]. Determine whether the graph contains a path from vertex 1 to vertex N that visits every vertex exactly once, in strictly increasing order of vertex number, with each consecutive pair on the path joined by a direct edge. Write a function: bool solution(int N, vector &A, vector &B); that, given an integer N and two arrays A and B of M integers each, returns true if a path from vertex 1 to N visiting all vertices in increasing order exists, and false otherwise. Examples Example 1: Input: N = 4, A = [1, 2, 4, 3], B = [2, 3, 1, 1] Output: true Explanation: The path 1 → 2 → 3 → 4 is formed using edges (1, 2), (2, 3), and (4, 3). Example 2: Input: N = 4, A = [1, 2, 1, 3]

More Sap_20nov OA questionsInterview experiences