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.
AnswerBot
4mo

Given a sorted N * N matrix, find the position of a target integer 'X'.

  • Use binary search in each row to narrow down the search space.

  • Start from the top-right corner or bottom-left corner for efficient...read more

Help your peers!
Select
Add answer anonymously...

Oracle Software Developer interview questions & answers

A Software Developer was asked 4mo agoQ. Design a data structure that supports insert and delete operations in O(1) time ...read more
A Software Developer was asked 6mo agoQ. How would you solve this bug?
A Software Developer was asked 6mo agoQ. Explain Linux system calls with an example.

Popular interview questions of Software Developer

A Software Developer was asked 6mo agoQ1. Explain Linux system calls with an example.
A Software Developer was asked 7mo agoQ2. What is testing? Explain the different types of testing.
A Software Developer was asked 9mo agoQ3. The count-and-say sequence is a sequence of digit strings defined by the recursi...read more

Top HR questions asked in Oracle Software Developer

A Software Developer was asked 5mo agoQ1. Tell me about your work experience.
A Software Developer was asked 5mo agoQ2. What are your strengths?
A Software Developer was asked 5mo agoQ3. Can you tell me about yourself?
Oracle Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits