Search in a 2D Matrix
Given a 2D matrix MAT
of size M x N, where M and N represent the number of rows and columns respectively. Each row is sorted in non-decreasing order, and the first element of each row is greater than the last element of the previous row if it exists. Your task is to determine if a given integer TARGET
exists within the matrix MAT
.
Input:
The first line contains an integer 'T', the number of test cases. For each test case, the first line contains three space-separated integers 'M', 'N', and 'TARGET'. Following this, every test case has 'M' lines with 'N' space-separated integers representing each row of the matrix 'MAT'.
Output:
For each test case, print "TRUE" if TARGET is found in the matrix MAT; otherwise, print "FALSE".
Example:
Sample Input:
2
3 3 78
1 2 4
6 7 8
9 10 34
2 2 8
1 1
4 8
Sample Output:
FALSE
TRUE
Explanation:
- For the first test case, TARGET = 78 is not present in the matrix.
- For the second test case, TARGET = 8 is present in the matrix.
Constraints:
- 1 <= T <= 102
- 1 <= N <= 50
- 1 <= M <= 50
- -105 <= MAT[i], TARGET <= 105
- Time Limit: 1 sec
Note:
No need to print anything; just implement the function.
Be the first one to answer
Add answer anonymously...
Top HSBC Group Software Engineer Trainee interview questions & answers
Popular interview questions of Software Engineer Trainee
Top HR questions asked in HSBC Group Software Engineer Trainee
>
HSBC Group Software Engineer Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app