Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
divide & impera
solve(A):
if A not a solution
then
solve(A[1..m][0..n])
solve(A[0..m][1..n])
solve(A[0..m-1][0..n])
solve(A[0..m][0..n-1])
else return A
begin with solve(A[0..m][0..n])
Speedups:
1. keep two LUT with number of ones on each line and column the computation is simpler o(1) for the number of 1 and 0 on every submatrix
2. in case that the size of the matrix is even skip computation and go directly to the next
replace all the 0's by -1's. take an auxiliary matrix of the same dimension (m X n). compute it's [i, j]-th entry as B[i,j] = A[i-1, j]+A[i, j-1]-A[i-1, j-1], where A is the transformed( all 0's replaced by -1's) original matrix and B our new aux matrix. the final part is to traverse B and find tuples (i, j) such that B[i, j] = 0 and compute the maximum i*j from those tuples. this can be done in O(m X n). lastly, convert all the -1's in A to 0's.
for 1 dimension array this problem can be solved with complexity O(m), (www(dot)geeksforgeeks(dot)org/archives/20586) m being the length of each row. if we make nc2 combination of rows and sum them up and apply the same algorithm as for 1D row. Hence Complexity O(n^2*m).
do not quite understand what you want to do when you say make nC2 combinations of rows and sum them up? can you please elaborate a little?
I'm not saying you can't get an algorithm with O(n^2*m) complexity, but this solution isn't quite right. You can't just calculate the max 1D solution for each row and then try every contiguous block of rows, if I'm understanding your idea correctly. That's because the parts of the rows you select also need to line up and form a rectangle.
The algo in the blog is cool....
One small change, we dont have to apply it for each row... instead convert the same algorithm to 2D using "summed area table"....
Search "summed area table" and read the wiki page for more details....
1. replace 0's with -1.
2. calculate the summed area table.
3. Hash each value in the table. If it is not in the table "add the 2D index" otherwise check whether the size of the rectangular box is greater than the max_size and update the max_size if necessary.
In worst case, the algorithm is O(mn) excluding the "summed area table" calculation.
- Please correct me if I am wrong
Thank you
@prabu: I don't think you've fully thought this through. The 2D case is more complicated than the 1D case.
In the 1D case, a subarray of sum 0 corresponded to a duplicate value in the sum array, and you could just use an algorithm for detecting duplicates. A duplicate in the sum array would correspond to a zero-sum subarray.
There is no such direct correspondence in the 2D case. Finding two identical values in the summed area table does not correspond to a rectangle whose sum is zero.
@eugene: Thank you for your feedback.... I did fail to analyze the case properly..... It is complicated ...
if you call the summed array as S then you will have to find 2 points (x1,y1) and (x2,y2) such that S[x1][y1] + S[x2][y2] - S[x1][y2] - S[x2][y1] = 0 and (x1 - x2)*(y1 -y2) is maximum for 0 <= x < m, 0 <= y < n. Even in that case you will have to consider the cases when x1 = x2 or y1 = y2 seperately (this corresponds to the case when the output array is a 1 D array). If you really try to do all this, it will become a O((mn)^2) algorithm again.
@Eugene..
The answer given in geeksforgeeks can be extended to a 2D (nXm) case.
Algo :
a. Substitute 0 by -1
b. For each columnwise, nC2 pairs exist.
The sum for each pair calculation will take O(n^2) and will vary from -n to +n (if we consider paid 1, n) at max.
c. As we traverse the matrix along a row, the sum can at max vary from -m*n to +m*n. So maintaining an array from -m*n to m*n and applying the method in geeksforgeeks the solution can be arrived at O(n^2*m).
This problem lends its self well to dynamic programming solution.
Note:
1.) Notice that the 3x3 solution rooted at A(i,j) is the same as the 2x2 solution with one more row and column added to the ends.
2.) Only valid solutions exist for even matrix.
javaish code:
For notation sake we declare that n > m; if this is not true, switch notion of n and m to make it true.
- Chris December 02, 2012Each operation should do no more additional work than 2(n-1)+1
That means that this takes O(n*((2n-1)+1)*m) work. This means that the operation works in O(n^2*m) time. Space is O(n*m) if we reuse (i,j) positions (This should be fine since we are preceding in a stepped fashion, first doing all s=1, then s= 2, ...).
I do not know of a better time algo. If you have one please share.
Disclaimer, uncompiled pseudocode