AfterAcademy Tech
•
18 Jul 2020

Difficulty: Medium
Asked in: Amazon, Microsoft
Problem Description
You are given a matrix arr of m x n size. Write a program to search for a value k in arr. This arr has the following properties:
If the value is found, then return 1 otherwise return 0.Example 1
Input:
arr[][] = [
[23, 25, 35, 37],
[40, 41, 42, 43],
[50, 60, 74, 80]
]
k = 41
Output: 1
Explanation: The value 41 is present in the array arr. So, 1 is returned.
Example 2
Input:
arr[][] = [
[23, 25, 35, 37],
[40, 41, 42, 43],
[50, 60, 74, 80]
]
k = 100
Output: 0
Explanation: The value 100 is not present in the array arr. So, 0 is returned.
We will be discussing various solutions for the problem
You may try this problem here.
The simple idea is to traverse the array and to compare the target element with all the elements in the matrix one by one.
Solution Step
Pseudo Code
bool searchMatrix(int[][] matrix, int target) {
if(matrix.empty())
return false
int n = matrix.size()
int m = matrix[0].size()
for(int i = 0 to i < m) {
for(int j = 0 to j < n)
//if the element is found
if(mat[i][j] == target) {
return true
}
}
return false
}
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(1)
Critical Ideas To Think
Start from the top right corner: row = 0, col = m-1
matrix[row][col] is equal target, return true.matrix[row][col] is less than the target, row = row + 1; indicates that this row can’t contain the target because this one in this line is the biggest one, counting from ‘row’.matrix[row][col] is greater than the target, col = col — 1; indicate that this column can’t contain the target because this one in this column is the smallest one, counting from ‘col’.Pseudo Code
bool searchMatrix(int[][] matrix, int target) {
if(matrix.empty())
return false
int n = matrix.size()
int m=matrix[0].size()
int row = 0,col = m-1
while(row < n and col >= 0){
if(matrix[row][col] == target)
return true
else if(matrix[row][col] < target)
row = row + 1
else
col = col - 1
}
return false
}
Complexity Analysis
Time Complexity: O(m + n)
Space Complexity: O(1)
Critical Ideas To Think
matrix[row][col] < target?The problem statement states that the values of the last col of the ith row is greater than the first col of (i+1)th row. Also, each row is sorted.
This means that, if will linearly arrange the elements of each row, we will have a sorted array. So we can now perform a binary search over it.
How will the matrix behave like an array without actually creating an auxiliary array?
It could be achieved by the following formula →
A n * m matrix converted into an array: matrix[x][y] → a[x * m + y]
An array can be converted into n * m matrix: a[x] → matrix[x / m][x % m]
Solution Steps
Pseudo Code
bool searchMatrix(int[][] matrix, int target) {
int n = matrix.size()
int m = matrix[0].size()
int l = 0, r = m * n - 1
while (l != r){
mid = (l + r - 1) / 2
if (matrix[mid / m][mid % m] < target)
l = mid + 1
else
r = mid
}
if( matrix[r / m][r % m] == target )
return true
else
return false
}
Complexity Analysis
Time Complexity: O(log(m*n)) = O(log(m) + log(n))
Space Complexity: O(1)
Critical Ideas To Think
r with m*n -1 ?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
In which situation 2 dimensional DP can be dropped to 1 dimension? Is there any principle or regular pattern? This is a very important question when it comes to optimization of DP arrays. Let's find out.

AfterAcademy Tech
Given a matrix or a 2-d array, you have to traverse the matrix in a spiral order. This is an interview question asked by companies like Microsoft and Amazon.

AfterAcademy Tech
Given a sorted integer array arr[] of n elements and a target value k, write a program to search k in arr[]. The famous binary search algorithm is easy to implement and the best optimization technique for performing searching operations

AfterAcademy Tech
Given a square matrix, turn it by 90 degrees in the clockwise direction.
