Question

In: Computer Science

Given an m × n matrix (or 2-dimensional array) whose rows and columns are sorted, so...

Given an m × n matrix (or 2-dimensional array) whose rows and columns are sorted, so
A[i][j]≤ A[i][j+1] and A[i][j]≤ A[i+1][j]
Write an algorithm that searches for a specific value in the matrix.

Solutions

Expert Solution

Algorithm

1. Start from topmost right element
2) Compare the given key with the element in matrix if the value match the key then print and just return
3) If the element in matrix is greater than the key then move left i.e by decrementing index j
4) If the element in matrix is smaller than the key then move down i.e by incrementing index i
5) If we end up reaching a stage where we reach out of bound and the element in matrix is not found then just print not found

We do not iterate the complete matrix, At max we will be visiting N element based on element is greater than or less than the key

Time Complexity = O(N)
Space Complexity = O(1)


Hence Time Complexity = O(N) and since we don't use any extra space Space Complexity = O(1)

Working Code in C

#include <stdio.h>

#define ROW 4

#define COL 4

void FindInSortedMatrix(int arr[ROW][COL], int key) {

int i = 0, j = ROW - 1;

while (i < ROW && j >= 0) {

if (arr[i][j] == key) {

printf("(%d, %d)\n", i+1, j+1);

return;

}

if (arr[i][j] > key)

j--;

else

i++;

}

printf("Not found\n");

}

int main()

{

int input[ROW][COL] = {

{ 2,29,37,  60 },

{ 9,  11, 33, 87 },

{ 19, 35, 17, 5 },

{ 22, 15, 91, 7 },

};

FindInSortedMatrix(input, 19);

return 0;

}




SEE OUTPUT



Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the number of rows and columns, respectively, define a function named sqabsmax that accepts array A as an argument (i.e. input parameter) and returns the square of the maximum absolute value element in A. Use the const qualifier if appropriate. Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sqabsmax. in C
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
c++ program to calculate the sum of the rows and the columns in a multidimensional array
c++ program to calculate the sum of the rows and the columns in a multidimensional array
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and...
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. Please do it simple. code
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Python - You are given a sorted (from smallest to largest) array A of n distinct...
Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.
A) Suppose owls is a MATLAB array with 251 rows and 51 columns representing the number...
A) Suppose owls is a MATLAB array with 251 rows and 51 columns representing the number of owls counted in the 251 counties in Texas had over the years 1960-2010. Write code to define a MATLAB variable to find the median number of owls counted in each county. B) Suppose cattle is a MATLAB array with 251 rows and 51 columns representing the number of cattle in the 251 counties in Texas had over the years 1950-2000. Write code to...
Write a program that read an array consists of 4 rows and 4 columns, then computes...
Write a program that read an array consists of 4 rows and 4 columns, then computes and print the sum of the elements above and below the main diagonal. (WITH C++)
Suppose C is a m × n matrix and A is a n × m matrix....
Suppose C is a m × n matrix and A is a n × m matrix. Assume CA = Im (Im is the m × m identity matrix). Consider the n × m system Ax = b. 1. Show that if this system is consistent then the solution is unique. 2. If C = [0 ?5 1 3 0 ?1] and A = [2 ?3   1 ?2    6 10] ,, find x (if it exists) when (a) b =[1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT