Largest Cycle in Maze Problem Statement
Given a maze represented by 'N' cells numbered from 0 to N-1, and an array arr
of 'N' integers where arr[i]
denotes the cell number that can be reached from the 'i'-th cell in one step, identify the length of the largest cycle in the maze.
Note that each cell may have ≤ 1 exit but can have multiple entry points. Cells may have self-cycles (a cycle of length 1). If arr[i] = -1
, it means the 'i'-th cell has no exit.
Example:
Input:
T = 2
N = 5
arr = [2, -1, 0, 4, 5]
N = 6
arr = [-1, 0, 1, 2, 3, -1]
Output:
3
-1
Explanation:
In the first test case, there is a cycle 0 -> 2 -> 0 with length 3. In the second test case, there are no cycles.
Constraints:
- 1 ≤ T ≤ 50
- 1 ≤ N ≤ 10,000
- -1 ≤ arr[i] < N-1
Note:
For each test case, output the length of the largest cycle in the maze or -1 if no cycles exist. Print the output for each test case on a new line.
Be the first one to answer
Add answer anonymously...
Top JUSPAY Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app