AfterAcademy Tech
•
06 Apr 2020

Difficulty: Medium
Asked in: Amazon
Given a N*M matrix which is sorted row-wise, you task is to find the median of the given matrix.
Median: The median of a group of ordered number is the middle number that will separate the highest half with lowest half of numbers. If there are two middle numbers the, the median is the mean of the numbers.
For example:
Input: 2 3 3
1 5 6
6 6 7
Output: 5
Explanation: The total number of elements is 9(odd), in this case the formula for finding the median is (1+N*M)/2 th smallest element out of the given elements. Here it is 5 which is the 5th smallest element as 1 2 3 and 3 are smaller than it.
Possible questions to ask the interviewer: →
You can first try to solve this problem here.
We are going to discuss two different approaches to solve this problem. First, we will look at the Brute Force Solution and then move on to optimize the solution using a better algorithm.
Solution idea
Here is it given that the total number of elements is always odd. Now, we have the formula that the (1+N*M)/2 th smallest element in an ordered(sorted) list is the median. Since the number of elements are odd there is no case of having more than one middle elements.We can think of a very simple approach where we will store all the elements of the matrix in a 1-Dimensional array and after sorting we will output the element at [(1+N*M)/2–1]th index.
Solution steps
Pseudo-code
int medianRowwiseSortedMatrix(int matrix[N][M])
{
int tempArr[N*M]
int desired_index = (1+N*M)/2 -1
int index = 0
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
tempArr[index++] = matrix[i][j]
}
}
sort(tempArr, tempArr + N*M)
return tempArr[desired_index]
}
Complexity Analysis
Time Complexity: O(N*M*log(N*M)
Space Complexity: O(N*M)
Critical ideas to think!
Solution idea
The idea here is to leverage the binary search algorithm in order to optimize this problem. If you have paid attention to the definition of median, you must have noticed in case of odd elements the median is (1+N*M)/2 th smallest number which basically implies that (1+N*M)/2 is the total number of elements which are smaller or equal to our median.One more thing to analyse is, the median will always lie in the range of minimum and maximum element.Using these observations and applying binary search for elements in the above range (minimum, maximum) and maintaing the counter for smaller or equal numbers for each element in this range, we can find our median(discussed above based on count of smaller or equal elements).
Solution idea
Let us see a step-by-step approach to this solution:
Pseudo-code
int medianRowwiseSortedMatrix(int matrix[N][M])
{
int min = INT_MAX;
int max = INT_MIN;
int desired_count = (1+(N*M)/2))
for(int i=0;i<N;i++)
{
if(A[i][0]<min)
min = A[i][0];
if(A[i][M-1]>max)
max = A[i][M-1];
}
int counter =0;
while(min<max)
{
counter=0;
int mid = (max+min)/2;
for(int i= 0;i<N;i++)
{
counter += upper_bound(A[i], A[i]+M, mid) - A[i];
}
if(counter < desired_count)
min = mid+1;
else
max = mid;
}
return min;
}
Note:- The upper_bound function used above is with reference to C++ STL. This basically gives the iterator pointing to the first element strictly greater than its third parameter(mid here). Can read about it here.
Complexity Analysis
Time Complexity: O(N*log(M)) (Why?)
Space Complexity: O(1)

Happy Coding!
AfterAcademy Tech
This is an interview problem asked in companies like Google, Microsoft, Amazon. The problem is to remove duplicates from a sorted array or list.

AfterAcademy Tech
Sort a linked list using insertion sort and return its head.

AfterAcademy Tech
This is an interview problem asked by companies like Myntra, Visa, Oracle, Adobe, etc. The problem is to remove the duplicate nodes from the given Linked List so that only unique nodes(with unique data values) are present.

AfterAcademy Tech
Given a matrix, A of size M x N of 0's and 1's. If an element is 0, set its entire row and column to 0
