-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec9_prefix_sum_2darray.java
91 lines (77 loc) · 2.66 KB
/
lec9_prefix_sum_2darray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package shreya.java;
import java.util.Scanner;
public class lec9_prefix_sum_2darray {
static int findsum (int [][] matrix,int l1,int r1 ,int l2 , int r2){
int sum =0;
for(int i =l1;i<=l2;i++){
for(int j=r1;j<=r2;j++){
sum+= matrix[i][j];
}
}
return sum;
}
//calculate row wise and col wise sum
static void findprefixsummatrix(int[][]matrix){
int r = matrix.length;
int c = matrix[0].length;
//traverse horizontally to calculate row wise prefix sum
//matrix[i][j] = sumrectangle ((0,0) ,(i,j))
for(int i=0;i<r;i++){
for(int j=1;j<c;j++){
matrix[i][j]+=matrix[i][j-1];
}
}
//traverse vertically to calculate col wise sum
for(int j=0;j<c;j++){
for(int i=1;i<r;i++){
matrix[i][j]+=matrix[i-1][j]; // with is we are adding i-1 element in i row
}
}
}
static int findsum2 (int [][] matrix,int l1,int r1 ,int l2 , int r2){
int sum =0;
findprefixsummatrix(matrix);
for(int i=l1;i<=l2;i++){
//r1 to r2 sum for row i
if (r1 >= 1) {
sum+= matrix[i][r2]-matrix[i][r1-1];
}
else {sum+=matrix[i][r2];
}}
return sum;
}
static int findsum3(int [][] matrix,int l1,int r1 ,int l2 , int r2){
int ans =0;
int sum =0 , up =0 , left=0 ,leftup =0;
findprefixsummatrix(matrix);
sum = matrix[l2][r2];
if(r1>=1){
left = matrix[l2][r1-1];}
if(l1>=1)
up =matrix[l1-1][r2];
if(l1>=1&&r1>=1)
leftup =matrix[l1-1][r1-1];
ans =sum-up-left+leftup;
return ans;}
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("enter the no.of rows and cols");
int r = sc.nextInt();
int c = sc.nextInt();
int [][] matrix = new int [r][c];
int totalelements =r*c;
System.out.println("enter "+ totalelements+" elements");
for(int i=0;i<r;i++){
for (int j=0;j<c;j++){
matrix[i][j] =sc.nextInt();
}
}
System.out.println("enter rectangle boundaries l1, r1, l2, r2 ");
int l1 = sc.nextInt();
int r1 = sc.nextInt();
int l2 = sc.nextInt();
int r2 = sc.nextInt();
System.out.println("rectangle sum"+findsum(matrix,l1,r1,l2,r2));
System.out.println("rectangle sum"+findsum3(matrix,l1,r1,l2,r2));
}
}