|
| 1 | +# Addition of submatrix |
| 2 | +## Easy |
| 3 | +<div class="problems_problem_content__Xm_eO"><p><span style="font-size:18px">Given a matrix <strong>Arr</strong> of size <strong>N</strong> x <strong>M</strong>. You are given position of submatrix as <strong>X<sub>1</sub>, Y<sub>1</sub></strong> and <strong>X<sub>2</sub>, Y<sub>2</sub></strong> inside the matrix. Find the sum of all elements inside that submatrix. Here <strong>X<sub>1</sub>, Y<sub>1</sub>,</strong> <strong>X<sub>2</sub>, Y<sub>2</sub> </strong>are 1-based.</span></p> |
| 4 | + |
| 5 | +<p><span style="font-size:18px"><strong>Example 1:</strong></span></p> |
| 6 | + |
| 7 | +<pre><span style="font-size:18px"><strong>Input: |
| 8 | +</strong>N = 5 , M = 6 |
| 9 | +Arr[][] = {{1, 2, 3, 4, 5, 6}, |
| 10 | + {7, 8, 9, 10, 11, 12}, |
| 11 | + {13, 14, 15, 16, 17, 18}, |
| 12 | + {19, 20, 21, 22, 23, 24}, |
| 13 | + {25, 26, 27, 28, 29, 30}} |
| 14 | +X<sub>1</sub>=3, Y<sub>1</sub>=4, X<sub>2</sub>=4, Y<sub>2</sub>=5 |
| 15 | +<strong>Output:</strong> 78 |
| 16 | +<strong>Explanation:</strong> Sum from cell starting at |
| 17 | +position (3, 4) (1-based indexing) and |
| 18 | +ending at (4, 5) is 78. |
| 19 | +</span></pre> |
| 20 | + |
| 21 | +<p><span style="font-size:18px"><strong>Example 2:</strong></span></p> |
| 22 | + |
| 23 | +<pre><span style="font-size:18px"><strong>Input:</strong> |
| 24 | +N = 3, M = 3 |
| 25 | +Arr[][] = {{9, 8, 7},{4, 2, 1},{6, 5, 3}} |
| 26 | +X<sub>1</sub>=1, Y<sub>1</sub>=2, X<sub>2</sub>=3, Y<sub>2</sub>=3 |
| 27 | +<strong>Output:</strong> 26 |
| 28 | +<strong>Explanation:</strong> Sum from cell starting at |
| 29 | +position (1, 2) (1-based indexing) and |
| 30 | +ending at (3, 3) is 26. |
| 31 | +</span></pre> |
| 32 | + |
| 33 | +<p><span style="font-size:18px"><strong>Your Task: </strong><br> |
| 34 | +You don't need to read input or print anything. Your task is to complete the function <strong>subMatrixSum()</strong> which takes the array of booleans <strong>arr[][],</strong><strong> n,</strong> <strong>m, x1, y1, x2 </strong>and<strong> y2</strong> as parameters and returns an integer denoting the answer.</span></p> |
| 35 | + |
| 36 | +<p><span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(N*M)<br> |
| 37 | +<strong>Expected Auxiliary Space:</strong> O(1)</span></p> |
| 38 | + |
| 39 | +<p><span style="font-size:18px"><strong>Constraints:</strong><br> |
| 40 | +1 ≤ N, M ≤ 10<sup>3</sup><br> |
| 41 | +1 ≤ Arr[N][M] ≤ 10<sup>6</sup><br> |
| 42 | +1 <= X<sub>1</sub>, X<sub>2 </sub><= N<br> |
| 43 | +1 <= Y<sub>1</sub>, Y<sub>2 </sub><= M</span></p> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +</div> |
0 commit comments