-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path02_sum of_all_submatrix_approach_2.cpp
More file actions
164 lines (138 loc) · 4.67 KB
/
02_sum of_all_submatrix_approach_2.cpp
File metadata and controls
164 lines (138 loc) · 4.67 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Sum of All Submatrix from a given Matrix (Approach-2)
- Prefix Sum Matrix Approach
Time Complexity - O(n^4)
Space Complexity - O(n^2)
Sample Input: 2 2
1 1
1 1
Sample Output: 16
*/
#include <iostream>
using namespace std;
/* A simple solution is to find psa[i][j] by traversing and adding values from a[0][0] to a[i][j].
Time complexity of this solution is O(Row*Col*Row*Col).
*/
void prefixSumMatrix1(int **arr, int rows, int cols, int **psa){
// Copy first row of arr[][] to psa[][]
for (int col=0; col<cols; col++)
psa[0][col] = arr[0][col];
// Do row wise sum
for (int row=1; row<rows; row++)
for (int col=0; col<cols; col++)
psa[row][col] = arr[row][col] + psa[row-1][col];
// Do column wise sum
for (int row=0; row<rows; row++)
for (int col=1; col<cols; col++)
psa[row][col] += psa[row][col-1];
// Display values of psa array
cout << "Prefix Sum Matrix: \n";
for(int row=0; row<=rows-1; row++){
for(int col=0; col<=cols-1; col++){
cout << psa[row][col] << " ";
}
cout << endl;
}
}
/* An efficient solution is to use previously computed values to compute psa[i][j].
Here if we simply add psa[i][j-1] and psa[i-1][j], we get sum of elements from a[0][0] to a[i-1][j-1] twice,
So we subtract psa[i-1][j-1].
*/
void prefixSumMatrix2(int **arr, int rows, int cols, int **psa){
// corner case [row=col=0]
psa[0][0] = arr[0][0];
// For first row [row=0 & col>0]
for(int col=1; col<=cols-1; col++){
psa[0][col] = psa[0][col-1] + arr[0][col];
}
// For first col [row>0 & col=0]
for(int row=1; row<=rows-1; row++){
psa[row][0] = psa[row-1][0] + arr[row][0];
}
// General formula for other case (i,j)
for(int row=1; row<=rows-1; row++){
for(int col=1; col<=cols-1; col++){
psa[row][col] = psa[row-1][col] + psa[row][col-1] - psa[row-1][col-1] +arr[row][col];
}
}
// Display values of psa array
cout << "Prefix Sum Matrix: \n";
for(int row=0; row<=rows-1; row++){
for(int col=0; col<=cols-1; col++){
cout << psa[row][col] << " ";
}
cout << endl;
}
}
/* A O(1) time function to compute sum of a submatrix
between (tli, tlj) and (bri, brj) using prefixSumArray[][]
which is built by the prefixSumMatrix function
*/
int submatrixSum(int **psa, int tli, int tlj, int bri, int brj){
// result is now sum of elements between (0, 0) and (bri, brj)
int sum = psa[bri][brj];
// Remove elements between (0, 0) and (tli-1, brj)
if (tli > 0)
sum = sum - psa[tli-1][brj];
// Remove elements between (0, 0) and (bri, tlj-1)
if (tlj > 0)
sum = sum - psa[bri][tlj-1];
// Add psa[tli-1][tlj-1], as elements between (0, 0) & (tli-1, tlj-1) are subtracted twice
if (tli > 0 && tlj > 0)
sum = sum + psa[tli-1][tlj-1];
return sum;
}
// Function to calculate Sum of All Submatrix
void sumOfAllSubmatrix(int **psa, int rows, int cols){
int sum = 0;
// All possible pairs of top left corner
for(int tli=0; tli<=rows-1; tli++){ // tli: Top Left Row Number
for(int tlj=0; tlj<=cols-1; tlj++){ // tlj: Top Left Column number
// All possible pairs of bottom right corner
for(int bri=tli; bri<=rows-1; bri++){ // bri: Bottom Right Row Number
for(int brj=tlj; brj<=cols-1; brj++){ // brj: Bottom Right Column Number
// sum up the elements
sum += submatrixSum(psa,tli,tlj,bri,brj);
}
}
}
}
cout << "Sum of Sub Matrix: " << sum;
}
// Drive code
int main(){
int rows, cols;
cout << "Enter matrix [Rows & Columns]: ";
cin >> rows >> cols;
int **arr = new int*[rows];
int **prefixSumArr = new int*[rows];
for(int idx=0; idx<=rows-1; idx++){
arr[idx] = new int[cols];
prefixSumArr[idx] = new int[cols];
}
// Taking matrix input
cout << "Enter matrix values: \n";
for(int row=0; row<=rows-1; row++){
for(int col=0; col<=cols-1; col++){
cin >> arr[row][col];
}
}
// calculating prefix sum matrix
prefixSumMatrix2(arr, rows, cols, prefixSumArr);
// prefixSumMatrix1(arr, rows, cols, prefixSumArr);
// calculating sum of submatrix
sumOfAllSubmatrix(prefixSumArr,rows,cols);
cout << endl;
return 0;
}
/*
OUTPUT:
Enter matrix [Rows & Columns]: 2 2
Enter matrix values:
1 2
3 4
Prefix Sum Matrix:
1 3
4 10
Sum of Sub Matrix: 40
*/