Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1079985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:57:31+00:00 2026-05-16T21:57:31+00:00

I have a x by y matrix, where each row and each column are

  • 0

I have a x by y matrix, where each row and each column are in ascending order as given below.

1   5   7    9
4   6   10   15
8   11  12   19
14  16  18   21

How to search this matrix for a number in O(x+y)?

I was asked this question for an interview, but could not figure out the way. Curious to know if it could be done.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:57:32+00:00Added an answer on May 16, 2026 at 9:57 pm

    Start at the last element of the first row(top-right corner).
    Compare it with the key. We have 3 cases:

    • If they are equal we are done.

    • If key is greater than that element
      then it means key cannot be present
      in that row so move the search
      to the element below it.

    • If key is less than that element then
      it means key could be present in that
      row towards left and cannot be present in the column further down, so move the search
      to the element left of it.

    Keep doing it till you find the element or you cannot further move(key does not exist).

    Pseudo code:

    Let R be number of rows
    Let C be number of columns
    
    Let i = 0
    Let j = C-1
    
    found = false
    while( i>=0 && i<R) && (j>=0 && j<C) )
       if (matrix[i][j] == key )
          found = true
          break
       else if( matrix[i][j] > key )
           j--
       else if( matrix[i][j] < key )
           i++
    end-while
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a m x n matrix where each row consists of zeros and
I have a matrix of data (with row names and column names). I would
I have several blocks of the following code that each use there own matrix.
just a quick question, if I have a matrix has n rows and m
The thing is I have to create a 2D matrix in php where each
I have a (symmetric) matrix M that represents the distance between each pair of
I have a matrix in SQL reporting and I would like it to print
I have a 30000x14000 sparse matrix in MATLAB (version 7), which I need to
I have a sparsely populated matrix that's the result of a series of left
I have a class to parse a matrix that keeps the result in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.