Search in a Row-wise and Column-wise Sorted Matrix Problem Statement
You are given an N * N matrix of integers where each row and each column is sorted in increasing order. Your task is to find the position of a given target integer 'X' within the matrix. If 'X' exists in the matrix, return the pair {i, j} where 'i' is the row index and 'j' is the column index of 'X'. If 'X' does not exist, return {-1, -1}.
Example:
Input:
Matrix: [ [1, 2, 5], [3, 4, 9], [6, 7, 10] ]
Target: 4
Output:
{1, 1}
Explanation:
The target 4 is located at position A[1][1] in the matrix.
Input:
The first line of input contains a single integer 'T', representing the number of test cases. Each test case contains:
- Two space-separated integers 'N' and 'X', representing the size of the matrix and the target element respectively.
- Followed by 'N' lines, each containing 'N' space-separated integers representing the rows of the matrix.
Output:
For each test case, output the position of 'X' if it is found, otherwise output “-1 -1”.
Constraints:
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 103
- 1 ≤ X ≤ 106
- 1 ≤ Aij ≤ 106
Note:
The matrix is guaranteed to have distinct elements. You only need to implement the function; printing the output is handled elsewhere.
Software testing is the process of evaluating a software item to detect differences between given input and expected output.
Software testing ensures that the software meets the requirements and works ...read more
Software testing is the process of evaluating a software item to detect differences between given input and expected output.
Software testing ensures that the software is working as expected
It helps to...read more
Top Amazon Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amazon Software Developer
Reviews
Interviews
Salaries
Users/Month