## Ankit Tripathi

BAN USER- 0of 0 votes

AnswersA 2D matrix is given to you. Now user will give 2 positions (x1,y1) and (x2,y2),

- Ankit Tripathi in India

which is basically the upper left and lower right coordinate of a rectangle formed within the matrix.

You have to print sum of all the elements within the area of rectangle in O(1) running time.

Now you can do any pre computation with the matrix.

But when it is done you should answer all your queries in constant time.

Example : consider this 2D matrix

1 3 5 1 8

8 3 5 3 7

6 3 9 6 0

Now consider 2 points given by user (0, 2) and (2, 4).

Your solution should print: 44.

i.e., the enclosed area is

5 1 8

5 3 7

9 6 0| Report Duplicate | Flag | PURGE

Snapdeal SDE1 Matrix

Correct explanation

- Ankit Tripathi November 28, 2014This is the most appropriate solution for parenthesis problem,

doesn't matter whether you use more than 1 parenthesis.

For more than 1 parenthesis, one must use a stack

- Ankit Tripathi December 31, 2012I think ANONYMOUS is right, i mean if there is a snake having its mouth in the cells from 51 to 100, then we would always try to get at that position where its mouth is and thus will land into an infinite for loop in order to maximize the number of moves.

- Ankit Tripathi December 31, 2012**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Hmm,

- Ankit Tripathi December 24, 2014I think this approach will work.

Let me code and verify it

Thanks :)