Given an array of integers, a number and a maximum value, task is to compute the maximum value that can be obtained from the array elements. Every value on the array traversing from the beginning can be either added to or subtracted from the result obtained from previous index such that at any point the result is not less than 0 and not greater than the given maximum value. For index 0 take previous result equal to given number. In case of no possible answer print -1.

Example :

Input : arr[] = {2, 1, 7} Number = 3 Maximum value = 7 Output : 7 The order of addition and subtraction is: 3(given number) - 2(arr[0]) - 1(arr[1]) + 7(arr[2]). Input : arr[] = {3, 10, 6, 4, 5} Number = 1 Maximum value = 15 Output : 9 The order of addition and subtraction is: 1 + 3 + 10 - 6 - 4 + 5

**Prerequisite : **Dynamic Programming | Recursion.

**Naive Approach : **Use recursion to find maximum value. At every index position there are two choices, either add current array element to value obtained so far from previous elements or subtract current array element from value obtained so far from previous elements. Start from index 0, add or subtract arr[0] from given number and recursively call for next index along with updated number. When entire array is traversed, compare the updated number with overall maximum value of number obtained so far.

**Below is the implementation of above approach :**

**Output:**

9

**Time Complexity : ** O(2^n).

**Note : **For small values of n <= 20, this solution will work. But as array size increases, this will not be an optimal solution.
An **efficient** solution is to use Dynamic Programming. Observe that the value at every step is constrained between 0 and maxLimit and hence, the required maximum value will also lie in this range. At every index position, after arr[i] is added to or subtracted from result, the new value of result will also lie in this range. Lets try to build the solution backwards. Suppose the required maximum possible value is x, where 0 ≤ x ≤ maxLimit. This value x is obtained by either adding or subtracting arr[n-1] to/from the value obtained until index position n-2. The same reason can be given for value obtained at index position n-2 that it depends on value at index position n-3 and so on. The resulting recurrence relation can be given as :

Check can x be obtained from arr[0..n-1]: Check can x - arr[n-1] be obtained from arr[0..n-2] || Check can x + arr[n-1] be obtained from arr[0..n-2]

A boolean DP table can be created in which dp[i][j] is 1 if value j can be obtained using arr[0..i] and 0 if not. For each index position, start from j = 0 and move to value maxLimit, and set dp[i][j] either 0 or 1 as described above. Find the maximum possible value that can be obtained at index position n-1 by finding maximum j when i = n-1 and dp[n-1][j] = 1.

**Output:**

9

**Time Complexity : ** O(n*maxLimit), where n is the size of array and maxLimit is the given max value.

**Auxiliary Space : ** O(n*maxLimit), n is the size of array and maxLimit is the given max value.

**Optimization : ** The space required can be reduced to O(2*maxLimit). Note that at every index position, we are only using values from previous row. So we can create a table with two rows, in which one of the rows store result for previous iteration and other for the current iteration.