![]() |
VOOZH | about |
You are given a 2-D array. We have to traverse each and every cell of the given array by following the cell locations then return true else false. The value of each cell is given by (x, y) where (x, y) is also shown next following cell position. Eg. (0, 0) shows starting cell. And 'null' shows our final destination after traversing every cell.
Examples:
Input : { 0, 1 1, 2 1, 1
0, 2 2, 0 2, 1
0, 0 1, 0 null }
Output : false
Input : { 0, 1 2, 0
null 1, 0
2, 1 1, 1 }
Output : true
We take a visited array if we visit a cell then make its value true in the visited array so that we can capture the cycle in our grid for next time when we visit it again. And if we find null before completing the loop then it means we didn't traversed all the cell of given array.
Implementation:
true
Time Complexity : O(N)
Auxiliary Space : O(M*N)