Given an array of N positive integers. We are allowed to remove element from either of the two ends i.e from the left side or right side of the array. Each time we remove an element, score is increased by value of element * (number of element already removed + 1). The task is to find the maximum score that can be obtained by removing all the element.
Examples:
Input : arr[] = { 1, 3, 1, 5, 2 }.
Output : 43
Remove 1 from left side (score = 1*1 = 1)
then remove 2, score = 1 + 2*2 = 5
then remove 3, score = 5 + 3*3 = 14
then remove 1, score = 14 + 1*4 = 18
then remove 5, score = 18 + 5*5 = 43.
Input : arr[] = { 1, 2 }
Output : 5.
The idea is to use Dynamic Programming. Make a 2D matrix named dp[][] initialised with 0, where dp[i][j] denote the maximum value of score from index from index ito index j of the array. So, our final result will be stored in dp[0][n-1].
Now, value for dp[i][j] will be maximum of arr[i] * (number of element already removed + 1) + dp[i+ 1][j] or arr[j] * (number of element already removed + 1) + dp[i][j – 1].
Below is the implementation of this approach:
C++
// CPP program to find maximum score we can get // by removing elements from either end. #include <bits/stdc++.h> #define MAX 50 using namespace std; int solve(int dp[][MAX], int a[], int low, int high, int turn) { // If only one element left. if (low == high) return a[low] * turn; // If already calculated, return the value. if (dp[low][high] != 0) return dp[low][high]; // Computing Maximum value when element at // index i and index j is to be chosed. dp[low][high] = max(a[low] * turn + solve(dp, a, low + 1, high, turn + 1), a[high] * turn + solve(dp, a, low, high - 1, turn + 1)); return dp[low][high]; } // Driven Program int main() { int arr[] = { 1, 3, 1, 5, 2 }; int n = sizeof(arr) / sizeof(arr[0]); int dp[MAX][MAX]; memset(dp, 0, sizeof(dp)); cout << solve(dp, arr, 0, n - 1, 1) << endl; return 0; } |
Java
// Java program to find maximum score we can get // by removing elements from either end. public class GFG { static final int MAX = 50; static int solve(int dp[][], int a[], int low, int high, int turn) { // If only one element left. if (low == high) { return a[low] * turn; } // If already calculated, return the value. if (dp[low][high] != 0) { return dp[low][high]; } // Computing Maximum value when element at // index i and index j is to be chosed. dp[low][high] = Math.max(a[low] * turn + solve(dp, a, low + 1, high, turn + 1), a[high] * turn + solve(dp, a, low, high - 1, turn + 1)); return dp[low][high]; } // Driven Program public static void main(String args[]) { int arr[] = {1, 3, 1, 5, 2}; int n = arr.length; int dp[][] = new int[MAX][MAX]; System.out.println(solve(dp, arr, 0, n - 1, 1)); } } /*This code is contributed by 29AjayKumar*/ |
Python 3
# Python 3 program to find maximum # score we can get by removing # elements from either end. MAX = 50 def solve(dp, a, low, high, turn): # If only one element left. if (low == high): return a[low] * turn # If already calculated, # return the value. if (dp[low][high] != 0): return dp[low][high] # Computing Maximum value when element # at index i and index j is to be chosed. dp[low][high] = max(a[low] * turn + solve(dp, a, low + 1, high, turn + 1), a[high] * turn + solve(dp, a, low, high - 1, turn + 1)); return dp[low][high] # Driver Code if __name__ == "__main__": arr = [ 1, 3, 1, 5, 2 ] n = len(arr) dp = [[0 for x in range(MAX)] for y in range(MAX)] print(solve(dp, arr, 0, n - 1, 1)) # This code is contributed by ChitraNayal |
C#
// C# program to find maximum score we can get // by removing elements from either end. using System; class GFG { static int MAX = 50; static int solve(int[,] dp, int[] a, int low, int high, int turn) { // If only one element left. if (low == high) return a[low] * turn; // If already calculated, return the value. if (dp[low, high] != 0) return dp[low, high]; // Computing Maximum value when element at // index i and index j is to be chosed. dp[low,high] = Math.Max(a[low] * turn + solve(dp, a, low + 1, high, turn + 1), a[high] * turn + solve(dp, a, low, high - 1, turn + 1)); return dp[low, high]; } // Driven code static void Main() { int[] arr = new int[]{ 1, 3, 1, 5, 2 }; int n = arr.Length; int[,] dp = new int[MAX,MAX]; for(int i = 0; i < MAX; i++) for(int j = 0; j < MAX; j++) dp[i, j] = 0; Console.Write(solve(dp, arr, 0, n - 1, 1)); } } // This code is contributed by DrRoot_ |
PHP
<?php // PHP program to find maximum score we can // get by removing elements from either end. $MAX = 50 ; function solve($dp, $a, $low, $high, $turn) { // If only one element left. if ($low == $high) return $a[$low] * $turn; // If already calculated, return the value. if ($dp[$low][$high] != 0) return $dp[$low][$high]; // Computing Maximum value when element at // index i and index j is to be chosed. $dp[$low][$high] = max($a[$low] * $turn + solve($dp, $a, $low + 1, $high, $turn + 1), $a[$high] * $turn + solve($dp, $a, $low, $high - 1, $turn + 1)); return $dp[$low][$high]; } // Driver Code $arr = array(1, 3, 1, 5, 2 ) ; $n = count($arr) ; $dp = array() ; for($i = 0; $i < $MAX ; $i++) { $dp[$i] = array_fill($i, $MAX, 0) ; } echo solve($dp, $arr, 0, $n - 1, 1); // This code is contributed by Ryuga ?> |
Output:
43
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximize the sum of sum of the Array by removing end elements
- Maximize product of subarray sum with its minimum element
- Remove an element to maximize the GCD of the given array
- Maximize count of elements reaching the end of an Array
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Remove k corner elements to maximize remaining sum
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Sum of product of each element with each element after it
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Maximize array sum by X increments when each element is divided by 10
- Maximize 3rd element sum in quadruplet sets formed from given Array
- Maximize the sum of modulus with every Array element
- Maximize the number of indices such that element is greater than element to its left
- Maximum element in an array such that its previous and next element product is maximum
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Find an element in array such that sum of left array is equal to sum of right array
- Remove all occurrences of any element for maximum array sum
- Maximize the maximum subarray sum after removing atmost one element
- Sum and Product of minimum and maximum element of an Array

