Skip to content
Related Articles

# Maximum subarray sum in an array created after repeated concatenation | Set-2

• Difficulty Level : Hard
• Last Updated : 17 Jul, 2021

Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the largest sum of any contiguous subarray in the modified array formed by repeating the given array K times.

Examples:

Input: arr[] = {-1, 10, 20}, K = 2
Output: 59
Explanation:
After concatenating the array twice, the array modifies to {-1, 10, 20, -1, 10, 20}.
The subarray with the maximum sum is over the range [1, 5] i.e {10, 20, -1, 10, 20}.

Input: arr[] = {10, 20, -30, -1, 40}, K =10
Output: 391

Naive Approach: The simplest approach to solve the problem is discussed in Set-1.

Efficient Approach: The above approach can be optimized further based on the following observations:

1. If the sum of the array is greater than 0, then it will contribute to the answer. Otherwise, it is not good to include all array elements into the maximum subarray.
2. Suppose variables maxPrefix and maxSufix store the maximum prefix sum and maximum suffix sum of a twice repeated array.
3. Therefore, the maximum sum subarray can be formed by either of the following ways:
1. Appending the elements of the maxSufix of the array formed by combining the first two arrays, then appending the remaining N-2 arrays.
2. First, appending the N-2 arrays and then appending the elements of the maxPrefix of the array formed by combining the last two arrays.
3. Taking all the elements of the maximum sum subarray of a twice repeated array.

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++14

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find contiguous subarray with``// maximum sum if array is repeated K times``int` `maxSubArraySumRepeated(``int` `arr[], ``int` `N, ``int` `K)``{``    ``// Store the sum of the array arr[]``    ``int` `sum = 0;` `    ``// Traverse the array and find sum``    ``for` `(``int` `i = 0; i < N; i++)``        ``sum += arr[i];` `    ``int` `curr = arr[0];` `    ``// Store the answer``    ``int` `ans = arr[0];` `    ``// If K = 1``    ``if` `(K == 1) {` `        ``// Apply Kadane algorithm to find sum``        ``for` `(``int` `i = 1; i < N; i++) {``            ``curr = max(arr[i], curr + arr[i]);``            ``ans = max(ans, curr);``        ``}``        ``// Return the answer``        ``return` `ans;``    ``}` `    ``// Stores the twice repeated array``    ``vector<``int``> V;` `    ``// Traverse the range [0, 2*N]``    ``for` `(``int` `i = 0; i < 2 * N; i++) {``        ``V.push_back(arr[i % N]);``    ``}` `    ``// Stores the maximum suffix sum``    ``int` `maxSuf = V[0];` `    ``// Stores the maximum prefix sum``    ``int` `maxPref = V[2 * N - 1];` `    ``curr = V[0];` `    ``for` `(``int` `i = 1; i < 2 * N; i++) {``        ``curr += V[i];``        ``maxPref = max(maxPref, curr);``    ``}` `    ``curr = V[2 * N - 1];``    ``for` `(``int` `i = 2 * N - 2; i >= 0; i--) {``        ``curr += V[i];``        ``maxSuf = max(maxSuf, curr);``    ``}` `    ``curr = V[0];``    ``// Apply Kadane algorithm for 2 repeatation``    ``// of the array``    ``for` `(``int` `i = 1; i < 2 * N; i++) {``        ``curr = max(V[i], curr + V[i]);``        ``ans = max(ans, curr);``    ``}` `    ``// If the sum of the array is greater than 0``    ``if` `(sum > 0) {``        ``int` `temp = 1LL * sum * (K - 2);``        ``ans = max(ans, max(temp + maxPref, temp + maxSuf));``    ``}` `    ``// Return the answer``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `arr[] = { 10, 20, -30, -1, 40 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``int` `K = 10;` `    ``// Function Call``    ``cout << maxSubArraySumRepeated(arr, N, K);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to find contiguous subarray with``// maximum sum if array is repeated K times``static` `int` `maxSubArraySumRepeated(``int``[] arr, ``int` `N,``                                  ``int` `K)``{``    ` `    ``// Store the sum of the array arr[]``    ``int` `sum = ``0``;` `    ``// Traverse the array and find sum``    ``for``(``int` `i = ``0``; i < N; i++)``        ``sum += arr[i];` `    ``int` `curr = arr[``0``];` `    ``// Store the answer``    ``int` `ans = arr[``0``];` `    ``// If K = 1``    ``if` `(K == ``1``)``    ``{``        ` `        ``// Apply Kadane algorithm to find sum``        ``for``(``int` `i = ``1``; i < N; i++)``        ``{``            ``curr = Math.max(arr[i], curr + arr[i]);``            ``ans = Math.max(ans, curr);``        ``}``        ` `        ``// Return the answer``        ``return` `ans;``    ``}` `    ``// Stores the twice repeated array``    ``ArrayList V = ``new`  `ArrayList();` `    ``// Traverse the range [0, 2*N]``    ``for``(``int` `i = ``0``; i < ``2` `* N; i++)``    ``{``        ``V.add(arr[i % N]);``    ``}` `    ``// Stores the maximum suffix sum``    ``int` `maxSuf = V.get(``0``);` `    ``// Stores the maximum prefix sum``    ``int` `maxPref = V.get(``2` `* N - ``1``);` `    ``curr = V.get(``0``);` `    ``for``(``int` `i = ``1``; i < ``2` `* N; i++)``    ``{``        ``curr += V.get(i);``        ``maxPref = Math.max(maxPref, curr);``    ``}` `    ``curr = V.get(``2` `* N - ``1``);``    ``for``(``int` `i = ``2` `* N - ``2``; i >= ``0``; i--)``    ``{``        ``curr += V.get(i);``        ``maxSuf = Math.max(maxSuf, curr);``    ``}` `    ``curr = V.get(``0``);``    ` `    ``// Apply Kadane algorithm for 2 repeatation``    ``// of the array``    ``for``(``int` `i = ``1``; i < ``2` `* N; i++)``    ``{``        ``curr = Math.max(V.get(i), curr + V.get(i));``        ``ans = Math.max(ans, curr);``    ``}` `    ``// If the sum of the array is greater than 0``    ``if` `(sum > ``0``)``    ``{``        ``int` `temp = sum * (K - ``2``);``        ``ans = Math.max(ans, Math.max(temp + maxPref,``                                     ``temp + maxSuf));``    ``}` `    ``// Return the answer``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ` `    ``// Given Input``    ``int` `[]arr = { ``10``, ``20``, -``30``, -``1``, ``40` `};``    ``int` `N = arr.length;``    ``int` `K = ``10``;` `    ``// Function Call``    ``System.out.print(maxSubArraySumRepeated(arr, N, K));``}``}` `// This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;``class` `GFG {``    ``// Function to find contiguous subarray with``    ``// maximum sum if array is repeated K times``    ``static` `int` `maxSubArraySumRepeated(``int``[] arr, ``int` `N,``                                      ``int` `K)``    ``{``        ``// Store the sum of the array arr[]``        ``int` `sum = 0;` `        ``// Traverse the array and find sum``        ``for` `(``int` `i = 0; i < N; i++)``            ``sum += arr[i];` `        ``int` `curr = arr[0];` `        ``// Store the answer``        ``int` `ans = arr[0];` `        ``// If K = 1``        ``if` `(K == 1) {` `            ``// Apply Kadane algorithm to find sum``            ``for` `(``int` `i = 1; i < N; i++) {``                ``curr = Math.Max(arr[i], curr + arr[i]);``                ``ans = Math.Max(ans, curr);``            ``}``            ``// Return the answer``            ``return` `ans;``        ``}` `        ``// Stores the twice repeated array``        ``List<``int``> V = ``new` `List<``int``>();` `        ``// Traverse the range [0, 2*N]``        ``for` `(``int` `i = 0; i < 2 * N; i++) {``            ``V.Add(arr[i % N]);``        ``}` `        ``// Stores the maximum suffix sum``        ``int` `maxSuf = V[0];` `        ``// Stores the maximum prefix sum``        ``int` `maxPref = V[2 * N - 1];` `        ``curr = V[0];` `        ``for` `(``int` `i = 1; i < 2 * N; i++) {``            ``curr += V[i];``            ``maxPref = Math.Max(maxPref, curr);``        ``}` `        ``curr = V[2 * N - 1];``        ``for` `(``int` `i = 2 * N - 2; i >= 0; i--) {``            ``curr += V[i];``            ``maxSuf = Math.Max(maxSuf, curr);``        ``}` `        ``curr = V[0];``        ``// Apply Kadane algorithm for 2 repeatation``        ``// of the array``        ``for` `(``int` `i = 1; i < 2 * N; i++) {``            ``curr = Math.Max(V[i], curr + V[i]);``            ``ans = Math.Max(ans, curr);``        ``}` `        ``// If the sum of the array is greater than 0``        ``if` `(sum > 0) {``            ``int` `temp = sum * (K - 2);``            ``ans = Math.Max(ans, Math.Max(temp + maxPref,``                                         ``temp + maxSuf));``        ``}` `        ``// Return the answer``        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``// Given Input``        ``int``[] arr = { 10, 20, -30, -1, 40 };``        ``int` `N = arr.Length;``        ``int` `K = 10;` `        ``// Function Call``        ``Console.WriteLine(``            ``maxSubArraySumRepeated(arr, N, K));``    ``}``}` `// This code is contributed by ukasp.`

## Python3

 `# python 3 program for the above approach` `# Function to find contiguous subarray with``# maximum sum if array is repeated K times``def` `maxSubArraySumRepeated(arr, N, K):``  ` `    ``# Store the sum of the array arr[]``    ``sum` `=` `0` `    ``# Traverse the array and find sum``    ``for` `i ``in` `range``(N):``        ``sum` `+``=` `arr[i]` `    ``curr ``=` `arr[``0``]` `    ``# Store the answer``    ``ans ``=` `arr[``0``]` `    ``# If K = 1``    ``if` `(K ``=``=` `1``):``      ` `        ``# Apply Kadane algorithm to find sum``        ``for` `i ``in` `range``(``1``,N,``1``):``            ``curr ``=` `max``(arr[i], curr ``+` `arr[i])``            ``ans ``=` `max``(ans, curr)` `        ``# Return the answer``        ``return` `ans` `    ``# Stores the twice repeated array``    ``V ``=` `[]` `    ``# Traverse the range [0, 2*N]``    ``for` `i ``in` `range``(``2` `*` `N):``        ``V.append(arr[i ``%` `N])` `    ``# Stores the maximum suffix sum``    ``maxSuf ``=` `V[``0``]` `    ``# Stores the maximum prefix sum``    ``maxPref ``=` `V[``2` `*` `N ``-` `1``]` `    ``curr ``=` `V[``0``]` `    ``for` `i ``in` `range``(``1``,``2` `*` `N,``1``):``        ``curr ``+``=` `V[i]``        ``maxPref ``=` `max``(maxPref, curr)` `    ``curr ``=` `V[``2` `*` `N ``-` `1``]``    ``i ``=` `2` `*` `N ``-` `2``    ``while``(i >``=` `0``):``        ``curr ``+``=` `V[i]``        ``maxSuf ``=` `max``(maxSuf, curr)``        ``i ``-``=` `1` `    ``curr ``=` `V[``0``]``    ` `    ``# Apply Kadane algorithm for 2 repeatation``    ``# of the array``    ``for` `i ``in` `range``(``1``, ``2` `*` `N, ``1``):``        ``curr ``=` `max``(V[i], curr ``+` `V[i])``        ``ans ``=` `max``(ans, curr)` `    ``# If the sum of the array is greater than 0``    ``if` `(``sum` `> ``0``):``        ``temp ``=` `sum` `*` `(K ``-` `2``)``        ``ans ``=` `max``(ans, ``max``(temp ``+` `maxPref, temp ``+` `maxSuf))` `    ``# Return the answer``    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `    ``# Given Input``    ``arr ``=` `[``10``, ``20``, ``-``30``, ``-``1``, ``40``]``    ``N ``=` `len``(arr)``    ``K ``=` `10` `    ``# Function Call``    ``print``(maxSubArraySumRepeated(arr, N, K))``    ` `    ``# This code is contributed by ipg2016107.`

## Javascript

 ``
Output
`391`

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

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up