Search In A Row Wise And Column Wise Sorted Matrix

You are given an N * N matrix of integers where each row and each column is sorted in increasing order. You are given a target integer 'X'. Find the position of 'X' in the matrix, if it exists then return the pair {i, j} where 'i' represents the row and 'j' represents the column of the array, otherwise return {-1,-1}

For example: If the given matrix is:

[ [1, 2, 5],
  [3, 4, 9],
  [6, 7, 10]] 
We have to find the position of 4. We will return {1,1} since A[1][1] = 4.
Input Format:
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run. 
Then the 'T' test cases follow.
The first line of each test case contains two space-separated integers 'N' and 'X', representing the size of the matrix and the target element respectively.
Each of the next 'N' lines contains 'N' space-separated integers representing the elements of the matrix.
Output Format:
For each test case, print the position of 'X', if it exists, otherwise print “-1 -1”.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 10^3
1 ≤ X ≤ 10^6
1 ≤ Aij ≤ 10^6

where 'T' is the number of test cases, 'N' is the number of rows and columns, 'X' is the target value, and Aij is the elements of the matrix.
Time Limit : 1 sec 

Note:

It is guaranteed that the matrix contains distinct elements.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
AnswerBot
1y

This question asks to find the position of a target integer in a row-wise and column-wise sorted matrix.

  • Iterate through each row and column of the matrix

  • Compare the target integer with the current ele...read more

CodingNinjas
author
2y
Brute Force
  • Run a loop from i = 0 to N - 1, to check each row.
    • Run a loop from j = 0 to N - 1, to check each element of the row.
      • If there is a match, return {i, j}.
  • If the element is not found in the enti...read more
CodingNinjas
author
2y
Binary Search
  • Run a loop for each column, i.e. iterate through each column.
    • Using binary search on each row, find if the element exists in the current row.
    • Binary Search is done because each row is sorte...read more
CodingNinjas
author
2y
Optimal Solution
  • Start your search from the top-right corner of the matrix.
  • There can be three cases:
    • The current element is equal to target: return this position
    • The current element is greater than the t...read more
Add answer anonymously...
MindTickle Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter