# Maximum score assigned to a subsequence of numerically consecutive and distinct array elements

• Last Updated : 22 Dec, 2021

Given two arrays arr[] and brr[] of size N such that the array brr[] consists of scores associated with corresponding elements of the array arr[]. The task is to find the maximum possible sum of assigned scores of a subsequence of numerically consecutive and distinct elements of the array arr[].
Examples:

Input: arr[] = {1, 2, 3, 3, 3, 1}, brr[] = {-1, 2, 10, 20, -10, -9}
Output: 22
Explanation:
Distinct values from the array = {1, 2, 3}
Maximum value assigned to each element = {1: -1, 2: 2, 3: 20}
Select the elements at index 2 and 4 in arr[] which are {2, 3}.
Maximum score = 2 + 20 = 22.

Input: arr[] = {1, 2, 3, 2, 3, 1}, brr[] = {-1, 2, 10, 20, -10, -9}
Output: 32
Explanation: Selected subsequence is {arr, arr, arr} = {2, 3, 2}

Naive Approach: The simplest approach to solve the problem is to use recursion. Follow the steps below to solve the problem:

1. Generate all possible subsets of the given array arr[] such that the subset has unique and consecutive elements.
2. While generating the subsets in the above step, there are two possibilities for every element of either being added to the subsequence or not. Therefore, follow the steps:
• If the current element is differed by 1 from the previously selected element, add the element to the subsequence.
• Otherwise, proceed to the next element.
3. Update the maximum_score by considering both the above two possibilities.
4. Print the final value of maximum_score obtained after the complete traversal of the array.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;`` ` `// Function to find the maximum score``// with unique element in the subset``int` `maximumSum(``int` `a[], ``int` `b[], ``int` `n,``               ``int` `index, ``int` `lastpicked)``{``    ` `    ``// Base Case``    ``if` `(index == n)``        ``return` `0;`` ` `    ``int` `option1 = 0, option2 = 0;`` ` `    ``// Check if the previously picked``    ``// element differs by 1 from the``    ``// current element``    ``if` `(lastpicked == -1 ||``      ``a[lastpicked] != a[index])`` ` `        ``// Calculate score by including``        ``// the current element``        ``option1 = b[index] + maximumSum(``                             ``a, b, n,``                             ``index + 1,``                             ``index);`` ` `    ``// Calculate score by excluding``    ``// the current element``    ``option2 = maximumSum(a, b, n,``                         ``index + 1,``                         ``lastpicked);`` ` `    ``// Return maximum of the``    ``// two possibilities``    ``return` `max(option1, option2);``}`` ` `// Driver code``int` `main()``{``    ` `    ``// Given arrays``    ``int` `arr[] = { 1, 2, 3, 3, 3, 1 };``    ``int` `brr[] = { -1, 2, 10, 20, -10, -9 };``    ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ` `    ``// Function call``    ``cout << (maximumSum(arr, brr, N, 0, -1));``}` `// This code is contributed by rutvik_56`

## Java

 `// Java program for the above approach``import` `java.io.*;` `class` `GFG {` `    ``// Function to find the maximum score``    ``// with unique element in the subset``    ``public` `static` `int` `maximumSum(``        ``int``[] a, ``int``[] b, ``int` `n, ``int` `index,``        ``int` `lastpicked)``    ``{``        ``// Base Case``        ``if` `(index == n)``            ``return` `0``;` `        ``int` `option1 = ``0``, option2 = ``0``;` `        ``// Check if the previously picked``        ``// element differs by 1 from the``        ``// current element``        ``if` `(lastpicked == -``1``            ``|| a[lastpicked] != a[index])` `            ``// Calculate score by including``            ``// the current element``            ``option1``                ``= b[index]``                  ``+ maximumSum(a, b, n,``                               ``index + ``1``,``                               ``index);` `        ``// Calculate score by excluding``        ``// the current element``        ``option2 = maximumSum(a, b, n,``                             ``index + ``1``,``                             ``lastpicked);` `        ``// Return maximum of the``        ``// two possibilities``        ``return` `Math.max(option1, option2);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Given arrays``        ``int` `arr[] = { ``1``, ``2``, ``3``, ``3``, ``3``, ``1` `};``        ``int` `brr[] = { -``1``, ``2``, ``10``, ``20``, -``10``, -``9` `};` `        ``int` `N = arr.length;` `        ``// Function Call``        ``System.out.println(``            ``maximumSum(arr, brr,``                       ``N, ``0``, -``1``));``    ``}``}`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum score``# with unique element in the subset``def` `maximumSum(a, b, n, index, lastpicked):``    ` `    ``# Base Case``    ``if` `(index ``=``=` `n):``        ``return` `0` `    ``option1 ``=` `0``    ``option2 ``=` `0` `    ``# Check if the previously picked``    ``# element differs by 1 from the``    ``# current element``    ``if` `(lastpicked ``=``=` `-``1` `or``      ``a[lastpicked] !``=` `a[index]):` `        ``# Calculate score by including``        ``# the current element``        ``option1 ``=` `b[index] ``+` `maximumSum(a, b, n,``                                        ``index ``+` `1``,``                                        ``index)` `    ``# Calculate score by excluding``    ``# the current element``    ``option2 ``=` `maximumSum(a, b, n, index ``+` `1``,``                         ``lastpicked)` `# Return maximum of the``# two possibilities``    ``return` `max``(option1, option2)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given arrays``    ``arr ``=` `[ ``1``, ``2``, ``3``, ``3``, ``3``, ``1` `]``    ``brr ``=` `[ ``-``1``, ``2``, ``10``, ``20``, ``-``10``, ``-``9` `]` `    ``N ``=` `len``(arr)` `    ``# Function call``    ``print``(maximumSum(arr, brr, N, ``0``, ``-``1``))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for``// the above approach``using` `System;``class` `GFG{` `// Function to find the maximum score``// with unique element in the subset``public` `static` `int` `maximumSum(``int``[] a, ``int``[] b,``                             ``int` `n, ``int` `index,``                             ``int` `lastpicked)``{``  ``// Base Case``  ``if` `(index == n)``    ``return` `0;` `  ``int` `option1 = 0, option2 = 0;` `  ``// Check if the previously picked``  ``// element differs by 1 from the``  ``// current element``  ``if` `(lastpicked == -1 ||``      ``a[lastpicked] != a[index])` `    ``// Calculate score by including``    ``// the current element``    ``option1 = b[index] + maximumSum(a, b, n,``                                    ``index + 1,``                                    ``index);` `  ``// Calculate score by excluding``  ``// the current element``  ``option2 = maximumSum(a, b, n,``                       ``index + 1,``                       ``lastpicked);` `  ``// Return maximum of the``  ``// two possibilities``  ``return` `Math.Max(option1, option2);``}   ` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ``// Given arrays``  ``int` `[]arr = {1, 2, 3, 3, 3, 1};``  ``int` `[]brr = {-1, 2, 10, 20, -10, -9};` `  ``int` `N = arr.Length;` `  ``// Function Call``  ``Console.WriteLine(maximumSum(arr, brr,``                               ``N, 0, -1));``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``
Output:
`22`

Time Complexity: O(2N
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by using Dynamic Programming as the problem has Overlapping Subproblems. Below are the steps:

1. Initialize index as 0 and lastPicked as -1.
2. Initialize a 2D array say dp[][] to store the result of the subproblems.
3. The states of dp[][] will be the current index and last picked integer.
4. Calculate the score for both the possible options:
• Select the current element if the last picked integer is different from the current integer.
• Skip the current element move on to the next element.
5. Store the current state as the maximum of the value calculated above two-state.
6. Print the value of dp[index][lastPicked + 1] as the result after all the recursive calls end.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum``// score possible``int` `maximumSum(``int` `a[], ``int` `b[], ``int` `n,``               ``int` `index, ``int` `lastpicked,``               ``vector> dp)``{``    ` `    ``// Base Case``    ``if` `(index == n)``        ``return` `0;` `    ``// If previously occurred subproblem``    ``// occurred``    ``if` `(dp[index][lastpicked + 1] != -1)``        ``return` `dp[index][lastpicked + 1];` `    ``int` `option1 = 0, option2 = 0;` `    ``// Check if lastpicked element differs``    ``// by 1 from the current element``    ``if` `(lastpicked == -1 ||``      ``a[lastpicked] != a[index])``    ``{``        ` `        ``// Calculate score by including``        ``// the current element``        ``option1 = b[index] + maximumSum(a, b, n,``                                        ``index + 1,``                                        ``index, dp);``    ``}` `    ``// Calculate score by excluding``    ``// the current element``    ``option2 = maximumSum(a, b, n,``                         ``index + 1,``                         ``lastpicked, dp);` `    ``// Return maximum score from``    ``// the two possibilities``    ``return` `dp[index][lastpicked + 1] = max(option1,``                                           ``option2);``}` `// Function to print maximum score``void` `maximumPoints(``int` `arr[], ``int` `brr[], ``int` `n)``{``    ``int` `index = 0, lastPicked = -1;` `    ``// DP array to store results``    ``// Initialise dp with -1``    ``vector> dp(n + 5, vector<``int``>(n + 5, -1));` `    ``// Function call``    ``cout << maximumSum(arr, brr, n, index,``                       ``lastPicked, dp)``         ``<< endl;``}`` ` `// Driver code   ``int` `main()``{``    ` `    ``// Given arrays``    ``int` `arr[] = { 1, 2, 3, 3, 3, 1 };``    ``int` `brr[] = { -1, 2, 10, 20, -10, -9 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function call``    ``maximumPoints(arr, brr, N);` `   ``return` `0;``}` `// This code is contributed by divyeshrabadiya07`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG {` `    ``// Function to find the maximum``    ``// score possible``    ``public` `static` `int` `maximumSum(``        ``int``[] a, ``int``[] b, ``int` `n, ``int` `index,``        ``int` `lastpicked, ``int``[][] dp)``    ``{``        ``// Base Case``        ``if` `(index == n)``            ``return` `0``;` `        ``// If previously occurred subproblem``        ``// occurred``        ``if` `(dp[index][lastpicked + ``1``] != -``1``)``            ``return` `dp[index][lastpicked + ``1``];` `        ``int` `option1 = ``0``, option2 = ``0``;` `        ``// Check if lastpicked element differs``        ``// by 1 from the current element``        ``if` `(lastpicked == -``1``            ``|| a[lastpicked] != a[index]) {` `            ``// Calculate score by including``            ``// the current element``            ``option1 = b[index]``                      ``+ maximumSum(a, b, n,``                                   ``index + ``1``,``                                   ``index, dp);``        ``}` `        ``// Calculate score by excluding``        ``// the current element``        ``option2 = maximumSum(a, b, n,``                             ``index + ``1``,``                             ``lastpicked, dp);` `        ``// Return maximum score from``        ``// the two possibilities``        ``return` `dp[index][lastpicked + ``1``]``            ``= Math.max(option1, option2);``    ``}` `    ``// Function to print maximum score``    ``public` `static` `void` `maximumPoints(``        ``int` `arr[], ``int` `brr[], ``int` `n)``    ``{``        ``int` `index = ``0``, lastPicked = -``1``;` `        ``// DP array to store results``        ``int` `dp[][] = ``new` `int``[n + ``5``][n + ``5``];` `        ``// Initialise dp with -1``        ``for` `(``int` `i[] : dp)``            ``Arrays.fill(i, -``1``);` `        ``// Function call``        ``System.out.println(``            ``maximumSum(arr, brr, n,``                       ``index, lastPicked, dp));``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Given arrays``        ``int` `arr[] = { ``1``, ``2``, ``3``, ``3``, ``3``, ``1` `};``        ``int` `brr[] = { -``1``, ``2``, ``10``, ``20``, -``10``, -``9` `};` `        ``int` `N = arr.length;` `        ``// Function Call``        ``maximumPoints(arr, brr, N);``    ``}``}`

## Python3

 `# Python3 program for``# the above approach` `# Function to find the``# maximum score possible``def` `maximumSum(a, b, n, index,``               ``lastpicked, dp):` `    ``# Base Case``    ``if` `(index ``=``=` `n):``        ``return` `0` `    ``# If previously occurred``    ``# subproblem occurred``    ``if` `(dp[index][lastpicked ``+` `1``] !``=` `-``1``):``        ``return` `dp[index][lastpicked ``+` `1``]` `    ``option1, option2 ``=` `0``, ``0` `    ``# Check if lastpicked element differs``    ``# by 1 from the current element``    ``if` `(lastpicked ``=``=` `-``1` `or``        ``a[lastpicked] !``=` `a[index]):` `        ``# Calculate score by including``        ``# the current element``        ``option1 ``=` `(b[index] ``+``                   ``maximumSum(a, b, n,``                              ``index ``+` `1``,``                              ``index, dp))``   ` `    ``# Calculate score by excluding``    ``# the current element``    ``option2 ``=` `maximumSum(a, b, n,``                         ``index ``+` `1``,``                         ``lastpicked, dp)` `    ``# Return maximum score from``    ``# the two possibilities``    ``dp[index][lastpicked ``+` `1``] ``=` `max``(option1,``                                    ``option2)``    ``return` `dp[index][lastpicked ``+` `1``]` `# Function to print maximum score``def` `maximumPoints(arr, brr, n):` `    ``index ``=` `0``    ``lastPicked ``=` `-``1` `    ``# DP array to store results``    ``dp ``=``[[ ``-``1` `for` `x ``in` `range` `(n ``+` `5``)]``              ``for` `y ``in` `range` `(n ``+` `5``)]` `    ``# Function call``    ``print` `(maximumSum(arr, brr,``                      ``n, index,``                      ``lastPicked, dp))` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``  ` `    ``# Given arrays``    ``arr ``=` `[``1``, ``2``, ``3``, ``3``, ``3``, ``1``]``    ``brr ``=` `[``-``1``, ``2``, ``10``, ``20``, ``-``10``, ``-``9``]` `    ``N ``=` `len``(arr)` `    ``# Function Call``    ``maximumPoints(arr, brr, N)` `# This code is contributed by Chitranayal`

## C#

 `// C# program for``// the above approach``using` `System;``class` `GFG{` `// Function to find the maximum``// score possible``public` `static` `int` `maximumSum(``int``[] a, ``int``[] b,``                             ``int` `n, ``int` `index,``                             ``int` `lastpicked,``                             ``int``[,] dp)``{``  ``// Base Case``  ``if` `(index == n)``    ``return` `0;` `  ``// If previously occurred``  ``// subproblem occurred``  ``if` `(dp[index, lastpicked + 1] != -1)``    ``return` `dp[index, lastpicked + 1];` `  ``int` `option1 = 0, option2 = 0;` `  ``// Check if lastpicked element differs``  ``// by 1 from the current element``  ``if` `(lastpicked == -1 ||``      ``a[lastpicked] != a[index])``  ``{``    ``// Calculate score by including``    ``// the current element``    ``option1 = b[index] + maximumSum(a, b, n,``                                    ``index + 1,``                                    ``index, dp);``  ``}` `  ``// Calculate score by excluding``  ``// the current element``  ``option2 = maximumSum(a, b, n,``                       ``index + 1,``                       ``lastpicked, dp);` `  ``// Return maximum score from``  ``// the two possibilities``  ``return` `dp[index, lastpicked + 1] =``         ``Math.Max(option1, option2);``}` `// Function to print maximum score``public` `static` `void` `maximumPoints(``int` `[]arr,``                                 ``int` `[]brr,``                                 ``int` `n)``{``  ``int` `index = 0, lastPicked = -1;` `  ``// DP array to store results``  ``int` `[,]dp = ``new` `int``[n + 5, n + 5];` `  ``// Initialise dp with -1``  ``for``(``int` `i = 0; i < n + 5; i++)``  ``{``    ``for` `(``int` `j = 0; j < n + 5; j++)``    ``{``      ``dp[i, j] = -1;``    ``}``  ``}``  ``// Function call``  ``Console.WriteLine(maximumSum(arr, brr, n,``                               ``index, lastPicked,``                               ``dp));``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ``// Given arrays``  ``int` `[]arr = {1, 2, 3, 3, 3, 1};``  ``int` `[]brr = {-1, 2, 10, 20, -10, -9};` `  ``int` `N = arr.Length;` `  ``// Function Call``  ``maximumPoints(arr, brr, N);``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``
Output:
`22`

Time Complexity: O(N2
Auxiliary Space: O(N2)

My Personal Notes arrow_drop_up