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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; #define INF 1e9 // Function to find the maximum score // achieved by alternative minus-plus // of elements of a subsequence in // the given array 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 the maximum return max(dp1.back(), dp2.back()); } // Driver Code 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
// Java program for the above approach class GFG { static double INF = 1E9; // Function to find the maximum score // achieved by alternative minus-plus // of elements of a subsequence in // the given array 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 the maximum return Math.max(dp1[dp1.length - 1 ], dp2[dp2.length - 1 ]); } // Driver Code 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)); } } // This code is contributed by saurabh_jaiswal. |
Python3
# Python Program to implement # the above approach INF = 1e9 # Function to find the maximum score # achieved by alternative minus-plus # of elements of a subsequence in # the given array 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 the maximum return max (dp1[ len (dp1) - 1 ], dp2[ len (dp2) - 1 ]) # Driver Code arr = [ 2 , 3 , 6 , 5 , 4 , 7 , 8 ] N = len (arr) print (maxScore(arr, N)) # This code is contributed by Saurabh Jaiswal |
C#
// C# code to implement the above approach using System; class GFG { static double INF = 1E9; // Function to find the maximum score // achieved by alternative minus-plus // of elements of a subsequence in // the given array 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 the maximum return Math.Max(dp1[dp1.Length - 1], dp2[dp2.Length - 1]); } // Driver Code public static void Main() { int []arr = { 2, 3, 6, 5, 4, 7, 8 }; int N = arr.Length; Console.Write(maxScore(arr, N)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript Program to implement // the above approach let INF = 1e9 // Function to find the maximum score // achieved by alternative minus-plus // of elements of a subsequence in // the given array 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 the maximum return Math.max(dp1[dp1.length - 1], dp2[dp2.length - 1]); } // Driver Code let arr = [2, 3, 6, 5, 4, 7, 8] let N = arr.length; document.write(maxScore(arr, N)); // This code is contributed by Potta Lokesh </script> |
Output
10
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!