Question

input --> NxN matrix of integers where every row and column strictly increasing. How can I...

input --> NxN matrix of integers where every row and column strictly increasing.

How can I design a search algorithm to determine whether matrix contains a element x?

Can you also analyze it? PLEASE make efficient as possible.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the pseudocode/algorithm below.

Let x be the  element we’re trying to search for in the matrix, and e be the current element we’re processing in the array.

1) We would need to start with top right element.
2) Loop for each row and for each row, loop for each column:

....i) compare this element e with x
…ii) if e = x, then return position of e, since we found x in the given matrix.
…iii) if e > x then move left to check elements smaller than e (if out of bound of matrix, then break and return false)
…iv) if e < x then move below to check elements greater than e (if out of bound of matrix, then break and return false)
3) repeat the i), ii) and iii) until you find the element or return false

Time Complexity: O(n)

Add a comment
Know the answer?
Add Answer to:
input --> NxN matrix of integers where every row and column strictly increasing. How can I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The longest increasing subsequence problem can be formulated as follows: input: n, a positive int...

    The longest increasing subsequence problem can be formulated as follows: input: n, a positive integer, and the array A of n comparable elements output: array R that contains the longest increasing subsequence The second algorithm, Powerset, generates all possible subsequences of the array A and then tests each subsequence on whether it is in increasing order. The longest such subsequence is a solution to the problem. Circle the correct answer: a) The second algorithm uses exhaustive search and has O(n...

  • Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231...

    Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231 Output: 0 This sequence also does not have a majority element (note that the element 1 appears twice and hence is not a majority element). Problem Introduction Majority rule is a decision rule that selects the alternative which has a majority, that is, more than half the votes. Given a sequence of elements (1.02....,On, you would like to check whether it contains an element...

  • Let M be an n x n matrix with each entry equal to either 0 or 1. Let mij denote the entry in row i and column j. A diago...

    Let M be an n x n matrix with each entry equal to either 0 or 1. Let mij denote the entry in row i and column j. A diagonal entry is one of the form mii for some i. Swapping rows i and j of the matrix M denotes the following action: we swap the values mik and mjk for k = 1,2, ... , n. Swapping two columns is defined analogously. We say that M is rearrangeable if...

  • Hello I have an automata question could you help me? [1Points] Give a formal description of a Tu...

    Hello I have an automata question could you help me? [1Points] Give a formal description of a Turing Machine M  that takes two parameters: an integer and an array of integers and decides whether the given integer is an element of the array or not. You can assume that all the integers are between 0 and 9. The input string will be written on the tape of the Turing machine. The first square of the tape contains the integer, the...

  • C++,please help. I have no idea how to do that begining with zero parameter constructor. and...

    C++,please help. I have no idea how to do that begining with zero parameter constructor. and help me to test the method, Write a class template Square Matrix that implements a square matrix of elements of a specified type. The class template should be fully in the Square Matrix.h file. The class uses "raw" pointers to handle the dynamically allocated 2D-array. "Raw" pointers are the classic pointers that you've used before. In addition to it, you will need a variable...

  • Question 5: Marbles 5 marks This question requires solving an optimization grid below, where some grid...

    Question 5: Marbles 5 marks This question requires solving an optimization grid below, where some grid cells contain a marble. based on a simple game. Consider the 5 x5 proble Starting from an initial grid (which can be represented by a 2d array of boolean values in a program), A marble can be removed from the grid if and only if there is at least one other marble in the same row or column of the marble to be removed....

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...

    (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....

  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT