-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefixsummatrix.java
95 lines (89 loc) · 2.71 KB
/
prefixsummatrix.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
92
93
94
95
import java.util.Scanner;
public class prefixsummatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter row and column matrix 1");
int r1 = sc.nextInt();
int c1 = sc.nextInt();
int[][] vib = new int[r1][c1];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
vib[i][j] = sc.nextInt();
}
}
// call
System.out.println("enter l1,r1,l2,r2 ");
int l = sc.nextInt(); // row
int r = sc.nextInt(); // column
int ll = sc.nextInt();
int rr = sc.nextInt();
//\
int anss = sumpredetermine3(vib, l, r, ll, rr);
System.out.println(anss + " ii");
}
static void print(int[][] vib) {
for (int i = 0; i < vib.length; i++) {
for (int j = 0; j < vib[i].length; j++) {
System.out.print(vib[i][j] + " ");
}
System.out.println();
}
}
// 1st mwthod
static int sumbrute(int [][] vib, int l,int r, int ll , int rr) {
int sum = 0 ;
for (int i = l ; i <=ll; i ++) {
for (int j = r; j <=rr; j ++) {
sum += vib[i][j];
}
}
return sum;
}
// 2nd method
static void findprefix(int [][] vib ) {
int r1 = vib.length;
int c1= vib[0].length;
// row sum
for (int i = 0; i<r1; i++) {
for (int j =1; j < c1; j++) {
vib [i][j] += vib[i][j-1];
}
}
// column wise sum
for (int j = 0; j <c1; j ++) {
for (int i = 1; i <r1; i ++) {
vib [i][j] += vib [i-1][j];
}
}
}
static int sumpredetermine(int [][] vib, int l,int r, int ll , int rr) {
int sum = 0;
findprefix(vib);
for (int i = l ; i <= ll; i++) {
// r1 to r2 sum for row i
if (r >=1)
sum += vib [i][rr] - vib [i][r-1];
else {
sum += vib[i][rr];
}
}
return sum;
}
// 3rd method
static int sumpredetermine3(int [][] vib, int l,int r, int ll , int rr) {
int anss = 0, sum = 0, left = 0, leftup =0, up = 0;
findprefix(vib);
sum = vib[ll][rr];
if (r >=1) {
left = vib[ll][r - 1];
}
if (l >=1) {
up = vib [l-1][r];
}
if (l >=1 && r >=1) {
leftup= vib[l-1][r-1];
}
anss = sum - left -up + leftup;
return anss;
}
}