specific pair in matrix c 2b 2b

Solutions on MaxInterview for specific pair in matrix c 2b 2b by the best coders in the world

showing results for - "specific pair in matrix c 2b 2b"
Dominique
29 Jan 2020
1#include<iostream>
2#define N 5
3using namespace std;
4int findMaxValue(int matrix[][N]) {
5   int maxValue = -99999;
6   int arr_max[N][N];
7   arr_max[N-1][N-1] = matrix[N-1][N-1];
8   int max_val = matrix[N-1][N-1];
9   for (int j = N - 2; j >= 0; j--) {
10      if (matrix[N-1][j] > max_val)
11      max_val = matrix[N - 1][j];
12      arr_max[N-1][j] = max_val;
13   }
14   max_val = matrix[N - 1][N - 1];
15   for (int i = N - 2; i >= 0; i--) {
16      if (matrix[i][N - 1] > max_val)
17      max_val = matrix[i][N - 1];
18      arr_max[i][N - 1] = max_val;
19   }
20   for (int i = N-2; i >= 0; i--) {
21      for (int j = N-2; j >= 0; j--) {
22         if (arr_max[i+1][j+1] - matrix[i][j] > maxValue)
23         maxValue = arr_max[i + 1][j + 1] - matrix[i][j];
24         arr_max[i][j] = max(matrix[i][j],max(arr_max[i][j + 1],arr_max[i + 1][j]) );
25      }
26   }
27   return maxValue;
28}
29int main() {
30   int mat[N][N] = {
31      { 1, 2, -1, -4, -20 },
32      { -8, -3, 4, 2, 1 },
33      { 3, 8, 6, 1, 3 },
34      { -4, -1, 1, 7, -6 },
35      { 0, -4, 10, -5, 1 }
36   };
37   cout << "Maximum Value is " << findMaxValue(mat);
38}