Skip to content

Commit dab8de7

Browse files
authored
Merge pull request #356 from thegauravverma/add-matrix
Program to search a sorted Matrix
2 parents 7919092 + 3c5712f commit dab8de7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Program to search for a given number in a sorted 2D Array.A matrix is said to be sorted when it all the rows and columns are sorted
3+
Time complexity- O(n+m) | Space Complexity- O(1)
4+
*/
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
void searchmatrix(int a[5][6],int check)
8+
{
9+
int i=0;
10+
int j=6-1;
11+
while(i<5 && j>=0)
12+
{
13+
if(a[i][j]> check)
14+
j-=1;
15+
else if(a[i][j] < check)
16+
i+=1;
17+
else
18+
{
19+
cout<<"Position"<<endl;
20+
cout<<"Row-"<<i<<" "<<"Column-"<<j<<endl;
21+
return;
22+
}
23+
24+
}
25+
cout<<"Number not found"<<endl;
26+
}
27+
28+
int main()
29+
{
30+
int matrix[5][6]={{1,4,7,12,15,1000},{2,5,19,31,32,1001},{3,8,24,33,35,1002},{40,41,42,44,45,1003},{99,100,103,106,128,1004}};
31+
searchmatrix(matrix,44);
32+
}

0 commit comments

Comments
 (0)