So we iterate through table4[] the to get the maximum value which will be our required answer.

longlongintfindMaxValue(longlongintarr[], intn)

{

// If the array has less than 4 elements

if(n < 4)

{

printf("The array should have atlest 4 elements\n");

returnMIN;

}

// We create 4 DP tables

longlonginttable1[n+1], table2[n+1], table3[n+1],

table4[n+1];

// Initialsing all the tables to MIN

for(inti=0; i<=n; i++)

table1[i] = table2[i] = table3[i] =

table4[i] = MIN;

// Filling table1[]

for(inti=n-1; i>=0; i--)

table1[i] = max(table1[i+1], arr[i]);

// Filling table2[]

for(inti=n-1; i>=0; i--)

table2[i] = max(table2[i+1],

table1[i+1] - arr[i]);

// Filling table3[]

for(inti=n-1; i>=0; i--)

table3[i] = max(table3[i+1],

table2[i+1] + arr[i]);

// Filling table4[]

for(inti=n-1; i>=0; i--)

table4[i] = max(table4[i+1],

table3[i+1] - arr[i]);

// Find maximum value in table4[]

longlongintres = MIN;

for(i=0; i<=n-1; i++)

res = max(res, table4[i]);

return(res);

}

This problem is simple yet powerful. The problem can be generalized to any expression under the given conditions. Find the maximum value of arr[j] – 2*arr[i] + 3*arr[l] – 7*arr[k], such that i < j < k < l