Given an array and a 2d matrix.
You have to find if the array is present in the matrix or not.To do so, you can start from any point in the matrix and go in 4 directions
that is if you are at (i,j)
you can go to (i+1,j), (i-1,j), (i,j+1), (i,j-1)
.
Return the starting and ending pairs of indices if the array exists otherwise return false.
Suppose the given array is [ 1, 10, 22, 47, 33, 10]
and the matrix is as given below..then as we can see the array exists in the matrix and the starting and ending points respectively are: (2,2) and (3,4) [ 0-based indexing]
One of the techinque to solve this would be to run DFS from each cell in the matrix and check if the array exists or not but that method will be O(n^4) (because there might be n^2 iteration in each DFS run).
Is my Time complexity analysis correct? Also can we do better than this?