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 198823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:58:14+00:00 2026-05-11T16:58:14+00:00

Give an algorithm to find a given element x (give the co-ordinates), in an

  • 0

Give an algorithm to find a given element x (give the co-ordinates), in an n by n matrix where the rows and columns are monotonically increasing.

My thoughts:
Reduce problem set size.

In the 1st column, find the largest element <= x. We know x must be in this row or after (lower). In the last column of the matrix, find the smallest element >= x. We know x must be in this row or before. Do the same thing with the first and last rows of the matrix. We have now defined a sub-matrix such that if x is in the matrix at all, it is in this sub-matrix. Now repeat the algo on this sub-matrix… Something along these lines.

[YAAQ: Yet another arrays question.]

  • 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-11T16:58:14+00:00Added an answer on May 11, 2026 at 4:58 pm

    I think you cannot hope for more than O(N), which is attainable. (N is the width of the matrix).

    Why you cannot hope for more

    Imagine a matrix like this:

    0 0 0 0 0 0 ... 0 0 x
    0 0 0 0 0 0 ... 0 x 2
    0 0 0 0 0 0 ... x 2 2
    .....................
    0 0 0 0 0 x ... 2 2 2
    0 0 0 0 x 2 ... 2 2 2
    0 0 0 x 2 2 ... 2 2 2
    0 0 x 2 2 2 ... 2 2 2
    0 x 2 2 2 2 ... 2 2 2
    x 2 2 2 2 2 ... 2 2 2
    

    where x is an unknown number (not the same number, ie. it might be a different one in every column). To satisfy the monotonicity of the matrix, you can place any of 0, 1, or 2 in all of the x places. So, to find if there is 1 in the matrix, you have to check all the x places, and there are N of them.

    How to make it O(n)

    Imagine you have to find first column indicies with number > q (a given number) for all rows. You start in the upper right corner of the matrix; if the number you see is greater, you go left; else go down. End when you are in the last row. The points where you went down are the places you search for. If any of them have the number you search for, you’ve found it.

    This algorithm is O(n), because in each step, you either go left or down. Totally, it cannot go more than N times left and N times down. Therefore it’s O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Solution to a recursive problem (code kata) give an algorithm to find
I have the following problem on my homework: Give an O(n+m) algorithm to find
Given an array of n * n , give an O(n) algorithm to find
I am stuck with one of the algorithm homework problem. Can anyone give me
Here is the problem : Give a algorithm that takes a positive integer n
I have developed an algorithm to find factors of a given number. Thus it
Problem: Given a list of strings, find the substring which, if subtracted from the
Given a set of words, we need to find the anagram words and display
I face the following problem in a cryptographical application: I have given a set
Give a linear-time algorithm to test whether a tree has a perfect matching, that

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.