Maximum Subarray Sum possible by replacing an Array element by its Square
Last Updated :
14 Apr, 2023
Given an array a[] consisting of N integers, the task is to find the maximum subarray sum that can be obtained by replacing a single array element by its square.
Examples:
Input: a[] = {1, -5, 8, 12, -8}
Output: 152
Explanation: Replacing 12 by 144, the subarray {8, 144} generates the maximum possible subarray sum in the array.
Input: a[] = {-1, -2, -3}
Output: 9
Explanation:
Naive Approach: The simplest approach to solve the problem is to replace every element with its square and for each of them, find the maximum subarray sum using Kadane's algorithm. Finally, print the maximum possible subarray sum obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using Dynamic Programming. Follow the steps below to solve the problem:
- Initialize memoization table dp[][] where:
- dp[i][0]: Stores the maximum subarray sum that can be obtained including ith element and without squaring any array element.
- dp[i][1]: Stores the maximum subarray sum that can be including ith element and squaring one of the array elements
- Therefore, the recurrence relations are:
dp[i][0] = max(dp[i-1][0] + a[i], a[i]), that is, either extend the previous subarray ending at i - 1th index or start a new subarray from ith index.
dp[i][1] = max(a[i]2, dp[i-1][0] + a[i]2, dp[i-1][1] + a[i]), that is, either start new subarray from ith index or extend previous subarray by adding a[i]2 to dp[i - 1][0] or add a[i] to dp[i - 1][1]
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum subarray
// sum possible
int getMaxSum(int a[], int n)
{
int dp[n][2];
// Stores sum without squaring
dp[0][0] = a[0];
// Stores sum squaring
dp[0][1] = a[0] * a[0];
// Stores the maximum subarray sum
int max_sum = max(dp[0][0], dp[0][1]);
for(int i = 1; i < n; i++)
{
// Either extend the subarray
// or start a new subarray
dp[i][0] = max(a[i],
dp[i - 1][0] + a[i]);
// Either extend previous squared
// subarray or start a new subarray
// by squaring the current element
dp[i][1] = max(dp[i - 1][1] + a[i],
a[i] * a[i]);
dp[i][1] = max(dp[i][1],
dp[i - 1][0] +
a[i] * a[i]);
// Update maximum subarray sum
max_sum = max(max_sum, dp[i][1]);
max_sum = max(max_sum, dp[i][0]);
}
// Return answer
return max_sum;
}
// Driver Code
int32_t main()
{
int n = 5;
int a[] = { 1, -5, 8, 12, -8 };
// Function call
cout << getMaxSum(a, n) << endl;
return 0;
}
// This code is contributed by rutvik_56
Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG {
// Function to find the maximum subarray
// sum possible
public static int getMaxSum(int a[], int n)
{
int dp[][] = new int[n][2];
// Stores sum without squaring
dp[0][0] = a[0];
// Stores sum squaring
dp[0][1] = a[0] * a[0];
// Stores the maximum subarray sum
int max_sum = Math.max(dp[0][0], dp[0][1]);
for (int i = 1; i < n; i++) {
// Either extend the subarray
// or start a new subarray
dp[i][0] = Math.max(a[i],
dp[i - 1][0] + a[i]);
// Either extend previous squared
// subarray or start a new subarray
// by squaring the current element
dp[i][1] = Math.max(dp[i - 1][1] + a[i],
a[i] * a[i]);
dp[i][1]
= Math.max(dp[i][1],
dp[i - 1][0] + a[i] * a[i]);
// Update maximum subarray sum
max_sum = Math.max(max_sum, dp[i][1]);
max_sum = Math.max(max_sum, dp[i][0]);
}
// Return answer
return max_sum;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int a[] = { 1, -5, 8, 12, -8 };
// Function call
System.out.println(getMaxSum(a, n));
}
}
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum subarray
# sum possible
def getMaxSum(a, n):
dp = [[0 for x in range(2)]
for y in range(n)]
# Stores sum without squaring
dp[0][0] = a[0]
# Stores sum squaring
dp[0][1] = a[0] * a[0]
# Stores the maximum subarray sum
max_sum = max(dp[0][0], dp[0][1])
for i in range(1, n):
# Either extend the subarray
# or start a new subarray
dp[i][0] = max(a[i],
dp[i - 1][0] + a[i])
# Either extend previous squared
# subarray or start a new subarray
# by squaring the current element
dp[i][1] = max(dp[i - 1][1] + a[i],
a[i] * a[i])
dp[i][1] = max(dp[i][1],
dp[i - 1][0] +
a[i] * a[i])
# Update maximum subarray sum
max_sum = max(max_sum, dp[i][1])
max_sum = max(max_sum, dp[i][0])
# Return answer
return max_sum
# Driver Code
n = 5
a = [ 1, -5, 8, 12, -8 ]
# Function call
print(getMaxSum(a, n))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the maximum subarray
// sum possible
public static int getMaxSum(int []a, int n)
{
int [,]dp = new int[n, 2];
// Stores sum without squaring
dp[0, 0] = a[0];
// Stores sum squaring
dp[0, 1] = a[0] * a[0];
// Stores the maximum subarray sum
int max_sum = Math.Max(dp[0, 0], dp[0, 1]);
for(int i = 1; i < n; i++)
{
// Either extend the subarray
// or start a new subarray
dp[i, 0] = Math.Max(a[i],
dp[i - 1, 0] + a[i]);
// Either extend previous squared
// subarray or start a new subarray
// by squaring the current element
dp[i, 1] = Math.Max(dp[i - 1, 1] + a[i],
a[i] * a[i]);
dp[i, 1] = Math.Max(dp[i, 1],
dp[i - 1, 0] +
a[i] * a[i]);
// Update maximum subarray sum
max_sum = Math.Max(max_sum, dp[i, 1]);
max_sum = Math.Max(max_sum, dp[i, 0]);
}
// Return answer
return max_sum;
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
int []a = { 1, -5, 8, 12, -8 };
// Function call
Console.WriteLine(getMaxSum(a, n));
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the maximum subarray
// sum possible
function getMaxSum(a, n)
{
let dp = new Array(n);
// Loop to create 2D array using 1D array
for (var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
// Stores sum without squaring
dp[0][0] = a[0];
// Stores sum squaring
dp[0][1] = a[0] * a[0];
// Stores the maximum subarray sum
let max_sum = Math.max(dp[0][0], dp[0][1]);
for (let i = 1; i < n; i++) {
// Either extend the subarray
// or start a new subarray
dp[i][0] = Math.max(a[i],
dp[i - 1][0] + a[i]);
// Either extend previous squared
// subarray or start a new subarray
// by squaring the current element
dp[i][1] = Math.max(dp[i - 1][1] + a[i],
a[i] * a[i]);
dp[i][1]
= Math.max(dp[i][1],
dp[i - 1][0] + a[i] * a[i]);
// Update maximum subarray sum
max_sum = Math.max(max_sum, dp[i][1]);
max_sum = Math.max(max_sum, dp[i][0]);
}
// Return answer
return max_sum;
}
// Driver Code
let n = 5;
let a = [ 1, -5, 8, 12, -8 ];
// Function call
document.write(getMaxSum(a, n));
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Prefix-Suffix Sum approach :
- Firstly, create a prefix sum array such that at i max prefix sum is stored.
- Secondly, create a suffix sum array such that at i max suffix sum is stored.
- After that simply iterate and check for each i few conditions -:
These two conditions should be checked firstly.
- arr[0]*arr[0],
- arr[n-1]*arr[n-1]
Then check these 4 conditions for each i -:
- prefix[i-1] + arr[i]*arr[i] + suffix[i+1],
- arr[i]*arr[i] + suffix[i+1],
- prefix[i-1] + arr[i]*arr[i],
- arr[i]*arr[i],
Then max of all these will be the answer.
Below is the code to implement the same:
C++
#include <algorithm>
#include <iostream>
using namespace std;
// Function to find the maximum subarray sum possible
int getMaxSum(int arr[], int n)
{
int prefix[n];
prefix[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix[i] = max(arr[i], prefix[i - 1] + arr[i]);
}
int suffix[n];
suffix[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
suffix[i] = max(arr[i], suffix[i + 1] + arr[i]);
}
int max = arr[0] * arr[0];
max = std::max(max, arr[n - 1] * arr[n - 1]);
for (int i = 1; i < n - 1; i++) {
max = std::max(max, prefix[i - 1] + arr[i] * arr[i]
+ suffix[i + 1]);
max = std::max(max,
arr[i] * arr[i] + suffix[i + 1]);
max = std::max(max,
prefix[i - 1] + arr[i] * arr[i]);
max = std::max(max, arr[i] * arr[i]);
}
return max;
}
// Driver code
int main()
{
int n = 5;
int a[] = { 1, -5, 8, 12, -8 };
// Function Calling
cout << getMaxSum(a, n) << endl;
return 0;
}
Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG {
// Function to find the maximum subarray
// sum possible
public static int getMaxSum(int[] arr, int n)
{
int[] prefix = new int[n];
prefix[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix[i]
= Math.max(arr[i], prefix[i - 1] + arr[i]);
}
int[] suffix = new int[n];
suffix[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
suffix[i]
= Math.max(arr[i], suffix[i + 1] + arr[i]);
}
int max = arr[0] * arr[0];
max = Math.max(max, arr[n - 1] * arr[n - 1]);
for (int i = 1; i < n - 1; i++) {
max = Math.max(max, prefix[i - 1]
+ arr[i] * arr[i]
+ suffix[i + 1]);
max = Math.max(max,
arr[i] * arr[i] + suffix[i + 1]);
max = Math.max(max,
prefix[i - 1] + arr[i] * arr[i]);
max = Math.max(max, arr[i] * arr[i]);
}
return max;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int a[] = { 1, -5, 8, 12, -8 };
// Function call
System.out.println(getMaxSum(a, n));
}
}
// This code is contributed by vishalkumarsahu04
Python3
#Function to find maximum subarray sum possible after squaring
def getMaxSum(arr, n):
prefix = [0] * n
prefix[0] = arr[0]
#Prefix used to store max of (current value and prefix value at that index)
for i in range(1, n):
prefix[i] = max(arr[i], prefix[i - 1] + arr[i])
suffix = [0] * n
suffix[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
suffix[i] = max(arr[i], suffix[i + 1] + arr[i])
#max_val storing the max value of subarray sum after squaring
max_val = arr[0] * arr[0]
max_val = max(max_val, arr[n - 1] * arr[n - 1])
for i in range(1, n - 1):
max_val = max(max_val, prefix[i - 1]
+ arr[i] * arr[i]
+ suffix[i + 1])
max_val = max(max_val,
arr[i] * arr[i] + suffix[i + 1])
max_val = max(max_val,
prefix[i - 1] + arr[i] * arr[i])
max_val = max(max_val, arr[i] * arr[i])
return max_val
n = 5
a = [1, -5, 8, 12, -8]
#Function call
print(getMaxSum(a, n))
#This code is contributed by Tejas Gundale
JavaScript
// Javascript Program to implement
// the above approach
// Function to find the maximum subarray
// sum possible
function getMaxSum(arr, n)
{
let prefix = new Array(n);;
prefix[0] = arr[0];
for (let i = 1; i < n; i++) {
prefix[i] = Math.max(arr[i], prefix[i - 1] + arr[i]);
}
let suffix = new Array(n);
suffix[n - 1] = arr[n - 1];
for (let i = n - 2; i >= 0; i--) {
suffix[i] = Math.max(arr[i], suffix[i + 1] + arr[i]);
}
let max = arr[0] * arr[0];
max = Math.max(max, arr[n - 1] * arr[n - 1]);
for (let i = 1; i < n - 1; i++) {
max = Math.max(max, prefix[i - 1]
+ arr[i] * arr[i]
+ suffix[i + 1]);
max = Math.max(max,
arr[i] * arr[i] + suffix[i + 1]);
max = Math.max(max,
prefix[i - 1] + arr[i] * arr[i]);
max = Math.max(max, arr[i] * arr[i]);
}
return max;
}
// Driver Code
let n = 5;
let a = [1, -5, 8, 12, -8];
// Function call
console.log(getMaxSum(a, n));
// This code is contributed by Nidhi goel.
C#
using System;
class GFG {
// Function to find the maximum subarray
// sum possible
public static int GetMaxSum(int[] arr, int n)
{
int[] prefix = new int[n];
prefix[0] = arr[0];
for (int i = 1; i < n; i++)
{
prefix[i] = Math.Max(arr[i], prefix[i - 1] + arr[i]);
}
int[] suffix = new int[n];
suffix[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--)
{
suffix[i] = Math.Max(arr[i], suffix[i + 1] + arr[i]);
}
int max = arr[0] * arr[0];
max = Math.Max(max, arr[n - 1] * arr[n - 1]);
for (int i = 1; i < n - 1; i++)
{
max = Math.Max(max, prefix[i - 1] + arr[i] * arr[i] + suffix[i + 1]);
max = Math.Max(max, arr[i] * arr[i] + suffix[i + 1]);
max = Math.Max(max, prefix[i - 1] + arr[i] * arr[i]);
max = Math.Max(max, arr[i] * arr[i]);
}
return max;
}
// Driver Code
public static void Main(string[] args)
{
int n = 5;
int[] a = { 1, -5, 8, 12, -8 };
// Function call
Console.WriteLine(GetMaxSum(a, n));
}
}
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Maximum subarray sum possible after removing at most K array elements
Given an array arr[] of size N and an integer K, the task is to find the maximum subarray sum by removing at most K elements from the array. Examples: Input: arr[] = { -2, 1, 3, -2, 4, -7, 20 }, K = 1 Output: 26 Explanation: Removing arr[5] from the array modifies arr[] to { -2, 1, 3, -2, 4, 20 } Su
15+ min read
Maximize Array sum by replacing middle elements with min of Subarray corners
Given an array A[] of length N. Then your task is to output the maximum sum that can be achieved by using the given operation at any number of times (possibly zero): Select a subarray, whose length is at least 3.Replace all the middle elements (Except the first and last) with a minimum of elements a
7 min read
Maximum subarray sum by flipping signs of at most K array elements
Given an array arr[] of N integers and an integer K, The task is to find the maximum sub-array sum by flipping signs of at most K array elements. Examples: Input: arr[] = {-6, 2, -1, -1000, 2}, k = 2 Output: 1009 We can flip the signs of -6 and -1000, to get maximum subarray sum as 1009 Input: arr[]
9 min read
Maximize non decreasing Array size by replacing Subarray with sum
Given an array arr[] of size N. In one operation only one subarray can be selected and replaced with the sum of the subarray. The task is to find the maximum size of the array after making it non-decreasing.Examples:Input: N = 5, arr[] = {5, 1, 6, 6, 6}Output: 4Explanation: maximum size non-decreasi
15+ min read
Maximize Array sum by replacing any K elements by its modulo with any positive integer
Given an array of positive integer arr[], and a number K. the task is to maximize the sum of the array by replacing any K elements of the array by taking modulus with any positive integer which is less than arr[i] i.e, (arr[i] = arr[i]%X where X ⤠arr[i]). Examples: Input: arr[] = {5, 7, 18, 12, 11,
5 min read
Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
Given an array arr[] consisting of N integers, the task is to find the maximum sum of the squares of array elements possible from the given array by performing the following operations:Select any pair of array elements (arr[i], arr[j])Replace arr[i] by arr[i] AND arr[j]Replace arr[j] by arr[i] OR ar
14 min read
Maximize sum of an Array by flipping sign of all elements of a single subarray
Given an array arr[] of N integers, the task is to find the maximum sum of the array that can be obtained by flipping signs of any subarray of the given array at most once. Examples: Input: arr[] = {-2, 3, -1, -4, -2} Output: 8Explanation: Flipping the signs of subarray {-1, -4, -2} modifies the arr
9 min read
Maximum Subarray sum of A[] by adding any element of B[] at any end
Given two arrays A[] and B[] having lengths N and M respectively, Where A[] and B[] can contain both positive and negative elements, the task is to find the maximum sub-array sum obtained by applying the below operations till the B[] has no element left in it: Choose either the leftmost or rightmost
15 min read
Maximum subarray sum with same first and last element formed by removing elements
Given an array arr[] of N integers, the task is to find the maximum sum of subarray having length an at least 2 whose first and last elements are the same after removing any number of array elements. If there exists no such array, then print 0. Examples: Input: arr[] = {-1, -3, -2, 4, -1, 3}Output:
6 min read
Maximum sum of Array formed by replacing each element with sum of adjacent elements
Given an array arr[] of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.Examples: Input: arr = [4, 2, 1, 3] Output: 23 Explanation: Replacing each element of the original array with the sum of adjacent
9 min read