Remove array end element to maximize the sum of product
Last Updated :
06 Feb, 2023
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.
Approach #1
The idea is to use Dynamic Programming. Make a 2D matrix named dp[][] initialized with 0, where dp[i][j] denote the maximum value of score from index from index into 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 choosed.
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 choosed.
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 choosed.
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 choosed.
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 choosed.
$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
?>
JavaScript
<script>
// Javascript program to find maximum score we can get
// by removing elements from either end.
let MAX = 50;
function solve(dp, a, low, high,
turn) {
// If only one element left.
if (low == high) {
return Math.floor(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 choosed.
dp[low][high] = Math.max(Math.floor(a[low] * turn )+ solve(dp, a,
low + 1, high, turn + 1),
Math.floor(a[high] * turn) + solve(dp, a,
low, high - 1, turn + 1));
return dp[low][high];
}
// driver function
let arr = [1, 3, 1, 5, 2];
let n = arr.length;
let dp = new Array(MAX);
// Loop to create 2D array using 1D array
for (var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
for (var i = 0; i < dp.length; i++) {
for (var j = 0; j < dp.length; j++) {
dp[i][j] = 0;
}
}
document.write(solve(dp, arr, 0, n - 1, 1));
// This code is contributed by susmitakundugoaldanga.
</script>
Approach #2
Apart from DP, a more intuitive solution will be a Greedy approach, where we choose the optimal solution at each step going forward. An optimal step will be to keep larger element inside as long as possible so that it can have a higher multiplier and higher sum:
C++
#include <iostream>
using namespace std;
int main() {
int stk[] = { 1, 3, 1, 5, 2 };
int removed = 0;
int top = 0;
int sum = 0;
int bottom = sizeof(stk) / sizeof(int) - 1;
while (removed < sizeof(stk) / sizeof(int)) {
// checking for the smaller element and considering that as popped.
if (stk[top] <= stk[bottom]) {
sum += stk[top] * (removed + 1);
top += 1;
}
else {
sum += stk[bottom] * (removed + 1);
bottom -= 1;
}
removed += 1;
}
cout << sum << endl;
}
// This code is contributed by aadityaburujwale.
Python
stk = [1, 3, 1, 5, 2]
removed = 0
top = 0
sum = 0
bottom = len(stk)-1
while removed < len(stk):
# checking for the smaller element and considering that as popped.
if stk[top] <= stk[bottom]:
sum += stk[top]*(removed+1)
top += 1
else:
sum += stk[bottom]*(removed+1)
bottom -= 1
removed += 1
print(sum)
# Code contributed by Sanyam Jain
Java
// Java program for the above approach
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] stk = { 1, 3, 1, 5, 2 };
int removed = 0;
int top = 0;
int sum = 0;
int bottom = stk.length - 1;
while (removed < stk.length) {
// checking for the smaller element and considering that as popped.
if (stk[top] <= stk[bottom]) {
sum += stk[top] * (removed + 1);
top += 1;
}
else {
sum += stk[bottom] * (removed + 1);
bottom -= 1;
}
removed += 1;
}
System.out.print(sum);
}
}
// This code is contributed by lokeshmvs21.
C#
// C# program for the above approach
using System;
public class GFG {
static public void Main()
{
// Code
int[] stk = { 1, 3, 1, 5, 2 };
int removed = 0;
int top = 0;
int sum = 0;
int bottom = stk.Length - 1;
while (removed < stk.Length) {
// checking for the smaller element and
// considering that as popped.
if (stk[top] <= stk[bottom]) {
sum += stk[top] * (removed + 1);
top += 1;
}
else {
sum += stk[bottom] * (removed + 1);
bottom -= 1;
}
removed += 1;
}
Console.Write(sum);
}
}
// This code is contributed by lokeshmvs21.
JavaScript
function solve(stk)
{
let removed = 0;
let top = 0;
let sum = 0;
let x= stk.length;
let bottom = x - 1;
while (removed < x)
{
// checking for the smaller element and considering that as popped.
if (stk[top] <= stk[bottom]) {
sum += stk[top] * (removed + 1);
top += 1;
}
else {
sum += stk[bottom] * (removed + 1);
bottom -= 1;
}
removed += 1;
}
console.log(sum);
}
// driver code
let stk = [ 1, 3, 1, 5, 2 ];
solve(stk);
// This code is contributed by garg28harsh.
Time Complexity : O(n)
Space Complexity : O(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read