VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-2-d-array-completely-traversed-not-following-cell-values/

⇱ Find if a 2-D array is completely traversed or not by following the cell values - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find if a 2-D array is completely traversed or not by following the cell values

Last Updated : 18 Jul, 2022

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. 

👁 Image

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:


Output
true

Time Complexity : O(N) 
Auxiliary Space : O(M*N) 

Comment
Article Tags:
Article Tags: