Maximize subsequence sum after putting plus minus sign alternatively on elements
Last Updated :
07 Dec, 2021
Given an array arr[] of size N. The task is to find the maximum score that can be achieved by alternative minus – plus of elements of any subsequence in the array.
Example:
Input: arr[] = {2, 3, 6, 5, 4, 7, 8}, N = 7
Output: 10
Explanation: Pick the sequence as {2, 3, 6, 5, 4, 7, 8} = {6, 4, 8}. So, alternative minus – plus = (6 – 4 + 8) = 10.
Input: {9, 2, 4, 5, 3}, N =5
Output: 12
Approach: We can use Dynamic Programming method to solve the problem.
- Let dp1i ⇢ be the maximum possible sum of a subsequence on a prefix from the first i elements, provided that the length of the subsequence is odd. Similarly enter dp2i ⇢ only for subsequences of even length.
- Then dp1i and dp2i are easy to recalculate as :
- dp1i+1 = max(dp1i, dp2i+arri)
- dp2i+1 = max(dp2i, dp1i−arri)
- The initial values are dp10 = −∞, dp20 = 0 and the answer will be stored in max(dp1n, dp2n).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
int maxScore( int arr[], int N)
{
vector< int > dp1(N + 1);
vector< int > dp2(N + 1);
dp1[0] = -INF;
dp2[0] = 0;
for ( int i = 0; i < N; ++i) {
dp1[i + 1] = max(dp1[i], dp2[i] + arr[i]);
dp2[i + 1] = max(dp2[i], dp1[i] - arr[i]);
}
return max(dp1.back(), dp2.back());
}
int main()
{
int arr[] = { 2, 3, 6, 5, 4, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxScore(arr, N);
return 0;
}
|
Java
class GFG {
static double INF = 1E9;
public static int maxScore( int arr[], int N) {
int [] dp1 = new int [N + 1 ];
int [] dp2 = new int [N + 1 ];
dp1[ 0 ] = ( int ) -INF;
dp2[ 0 ] = 0 ;
for ( int i = 0 ; i < N; ++i) {
dp1[i + 1 ] = Math.max(dp1[i], dp2[i] + arr[i]);
dp2[i + 1 ] = Math.max(dp2[i], dp1[i] - arr[i]);
}
return Math.max(dp1[dp1.length - 1 ], dp2[dp2.length - 1 ]);
}
public static void main(String args[]) {
int arr[] = { 2 , 3 , 6 , 5 , 4 , 7 , 8 };
int N = arr.length;
System.out.println(maxScore(arr, N));
}
}
|
Python3
INF = 1e9
def maxScore(arr, N):
dp1 = [ 0 ] * (N + 1 )
dp2 = [ 0 ] * (N + 1 )
dp1[ 0 ] = - INF
dp2[ 0 ] = 0
for i in range (N):
dp1[i + 1 ] = max (dp1[i], dp2[i] + arr[i])
dp2[i + 1 ] = max (dp2[i], dp1[i] - arr[i])
return max (dp1[ len (dp1) - 1 ], dp2[ len (dp2) - 1 ])
arr = [ 2 , 3 , 6 , 5 , 4 , 7 , 8 ]
N = len (arr)
print (maxScore(arr, N))
|
C#
using System;
class GFG
{
static double INF = 1E9;
public static int maxScore( int []arr, int N) {
int []dp1 = new int [N + 1];
int []dp2 = new int [N + 1];
dp1[0] = ( int ) -INF;
dp2[0] = 0;
for ( int i = 0; i < N; ++i) {
dp1[i + 1] = Math.Max(dp1[i], dp2[i] + arr[i]);
dp2[i + 1] = Math.Max(dp2[i], dp1[i] - arr[i]);
}
return Math.Max(dp1[dp1.Length - 1], dp2[dp2.Length - 1]);
}
public static void Main() {
int []arr = { 2, 3, 6, 5, 4, 7, 8 };
int N = arr.Length;
Console.Write(maxScore(arr, N));
}
}
|
Javascript
<script>
let INF = 1e9
function maxScore(arr, N) {
let dp1 = new Array(N + 1);
let dp2 = new Array(N + 1);
dp1[0] = -INF;
dp2[0] = 0;
for (let i = 0; i < N; ++i) {
dp1[i + 1] = Math.max(dp1[i], dp2[i] + arr[i]);
dp2[i + 1] = Math.max(dp2[i], dp1[i] - arr[i]);
}
return Math.max(dp1[dp1.length - 1], dp2[dp2.length - 1]);
}
let arr = [2, 3, 6, 5, 4, 7, 8]
let N = arr.length;
document.write(maxScore(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...