Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mopuriiswaryalakshmi/2320532474afb7be0706d1086c5614fb to your computer and use it in GitHub Desktop.
Save mopuriiswaryalakshmi/2320532474afb7be0706d1086c5614fb to your computer and use it in GitHub Desktop.
Search an element in 2D Matrix
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