Created
April 26, 2024 09:14
-
-
Save mopuriiswaryalakshmi/2320532474afb7be0706d1086c5614fb to your computer and use it in GitHub Desktop.
Search an element in 2D Matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function linerSearch (matrix2D, target) { | |
for (i =0; i < matrix2D.length; i++){ | |
for(j=0; j< matrix2D[i].length; j++){ | |
// console.log(matrix2D[i][j]) | |
if(matrix2D[i][j] == target) { | |
return console.log([i, j]) | |
} | |
} | |
} | |
return console.log([-1, -1]) | |
} | |
let matrix2D = [ [1,2,3], | |
[4,5,6], | |
[7,8,9] ] | |
linerSearch(matrix2D, 7) | |
/*Time Complexity: O (N * M), | |
N is the number of rows and | |
M is the number of columns. | |
Auxiliary Space: O(1) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment