-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-matrix-sum.c
More file actions
45 lines (36 loc) · 1.02 KB
/
maximum-matrix-sum.c
File metadata and controls
45 lines (36 loc) · 1.02 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
#include <stdio.h>
#include <stdlib.h>
int mabs(int n) {
if (n < 0) return -n;
return n;
}
long long maxMatrixSum(int** matrix, int matrixSize, int* matrixColSize) {
long long sum = 0;
int even = 0;
int min = 1000000;
for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < matrixColSize[i]; j++) {
if (matrix[i][j] < 0) even++;
int elem = mabs(matrix[i][j]);
sum += elem;
if (elem < min) min = elem;
}
}
if (even % 2 != 0) {
sum-=2*min;
}
return sum;
}
int main() {
int rows = 3, cols = 3;
int **matrix = (int **)malloc(rows * sizeof(int *));
for(int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3;
matrix[1][0] = -1; matrix[1][1] = -2; matrix[1][2] = -3;
matrix[2][0] = 1; matrix[2][1] = 2; matrix[2][2] = 3;
long long sum = maxMatrixSum(matrix, rows, &cols);
printf("%d\n", sum);
return 0;
}