Sir, Is it possible to create a 2-D array and fill it by our observation.
That is, if i > j we will add 0;
if i == j we will fill arr[i] * year
and the last case
if i < j then we will take the max of(arr2[i-1][j], arr[i][j-1]) and multiply it by year.
I have tried this but wasn’t able to code the third case.
I need help here.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector vect;
for(int i = 0 ; i < n; i++) {
int temp;
cin >> temp;
vect.push_back(temp);
}
vector<vector<int>> grid;
for(int i = 0; i <= n; i++) {
vector<int> temp;
for(int j = 0; j <= n; j++){
temp.push_back(-1);
}
grid.push_back(temp);
}
int year = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i > j){
grid[i][j] = 0;
}else if(i == j) {
grid[i][j] = (n) * vect[i];
}
//I am unable to fill the upper diagonal
// else if(j > i) {
// grid[i][j] = max(grid[i-1][j], grid[i][j-1]);
// // + vect[i] * (j - i + 1);
// }
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << grid[i][j] << " ";
}cout << endl;
}
cout << grid[0][n-1] << endl;
}