Maximum subarray sum in an array created after repeated concatenation
Last Updated :
02 Nov, 2024
Given an array and a number k, find the largest sum of contiguous array in the modified array which is formed by repeating the given array k times.
Examples :
Input : arr[] = {-1, 10, 20}, k = 2
Output : 59
After concatenating array twice, we get {-1, 10, 20, -1, 10, 20} which has maximum subarray sum as 59.
Input : arr[] = {-1, -2, -3}, k = 3
Output : -1
Naive Approach - O(nk) Time and O(nk) Space
A simple solution is to create an array of size n*k, then run Kadane's algorithm for the whole array.
Expected Approach - O(nk) Time and O(1) Space
An efficient solution is to run a loop on same array and use modular arithmetic to move back beginning after end of array.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// Returns sum of maximum sum subarray created
// after concatenating a[0..n-1] k times.
int maxSubArraySumRepeated(vector<int>& arr, int k) {
int n = arr.size();
int maxSoFar = INT_MIN, maxEndingHere = 0;
for (int i = 0; i < n * k; i++) {
// Use modular arithmetic to get the next element
maxEndingHere += arr[i % n];
maxSoFar = max(maxSoFar, maxEndingHere);
if (maxEndingHere < 0)
maxEndingHere = 0;
}
return maxSoFar;
}
/* Driver program to test maxSubArraySum */
int main() {
vector<int> arr = {10, 20, -30, -1};
int k = 3;
cout << "Maximum contiguous sum is "
<< maxSubArraySumRepeated(arr, k);
return 0;
}
C
// Returns sum of maximum sum subarray created
// after concatenating a[0..n-1] k times.
#include <stdio.h>
#include <limits.h>
int maxSubArraySumRepeated(int arr[], int n, int k) {
int maxSoFar = INT_MIN, maxEndingHere = 0;
for (int i = 0; i < n * k; i++) {
// Use modular arithmetic to get the next element
maxEndingHere += arr[i % n];
if (maxEndingHere > maxSoFar) {
maxSoFar = maxEndingHere;
}
if (maxEndingHere < 0) {
maxEndingHere = 0;
}
}
return maxSoFar;
}
// Driver program to test maxSubArraySumRepeated
int main() {
int arr[] = {10, 20, -30, -1};
int k = 3;
int n = sizeof(arr) / sizeof(arr[0]);
printf("Maximum contiguous sum is %d\n", maxSubArraySumRepeated(arr, n, k));
return 0;
}
Java
// Returns sum of maximum sum subarray created
// after concatenating a[0..n-1] k times.
public class GfG {
public static int maxSubArraySumRepeated(int[] arr, int k) {
int n = arr.length;
int maxSoFar = Integer.MIN_VALUE, maxEndingHere = 0;
for (int i = 0; i < n * k; i++) {
// Use modular arithmetic to get the next element
maxEndingHere += arr[i % n];
maxSoFar = Math.max(maxSoFar, maxEndingHere);
if (maxEndingHere < 0)
maxEndingHere = 0;
}
return maxSoFar;
}
// Driver program to test maxSubArraySumRepeated
public static void main(String[] args) {
int[] arr = {10, 20, -30, -1};
int k = 3;
System.out.println("Maximum contiguous sum is " + maxSubArraySumRepeated(arr, k));
}
}
Python
# Returns sum of maximum sum subarray created
# after concatenating a[0..n-1] k times.
def max_subarray_sum_repeated(arr, k):
n = len(arr)
max_so_far = float('-inf')
max_ending_here = 0
for i in range(n * k):
# Use modular arithmetic to get the next element
max_ending_here += arr[i % n]
max_so_far = max(max_so_far, max_ending_here)
if max_ending_here < 0:
max_ending_here = 0
return max_so_far
# Driver program to test max_subarray_sum_repeated
arr = [10, 20, -30, -1]
k = 3
print("Maximum contiguous sum is", max_subarray_sum_repeated(arr, k))
C#
// Returns sum of maximum sum subarray created
// after concatenating a[0..n-1] k times.
using System;
using System.Linq;
class Program {
public static int MaxSubArraySumRepeated(int[] arr, int k) {
int n = arr.Length;
int maxSoFar = int.MinValue, maxEndingHere = 0;
for (int i = 0; i < n * k; i++) {
// Use modular arithmetic to get the next element
maxEndingHere += arr[i % n];
maxSoFar = Math.Max(maxSoFar, maxEndingHere);
if (maxEndingHere < 0)
maxEndingHere = 0;
}
return maxSoFar;
}
// Driver program to test MaxSubArraySumRepeated
static void Main() {
int[] arr = {10, 20, -30, -1};
int k = 3;
Console.WriteLine("Maximum contiguous sum is " + MaxSubArraySumRepeated(arr, k));
}
}
JavaScript
// Returns sum of maximum sum subarray created
// after concatenating a[0..n-1] k times.
function maxSubArraySumRepeated(arr, k) {
let n = arr.length;
let maxSoFar = Number.MIN_SAFE_INTEGER, maxEndingHere = 0;
for (let i = 0; i < n * k; i++) {
// Use modular arithmetic to get the next element
maxEndingHere += arr[i % n];
maxSoFar = Math.max(maxSoFar, maxEndingHere);
if (maxEndingHere < 0)
maxEndingHere = 0;
}
return maxSoFar;
}
// Driver program to test maxSubArraySumRepeated
let arr = [10, 20, -30, -1];
let k = 3;
console.log("Maximum contiguous sum is " + maxSubArraySumRepeated(arr, k));
OutputMaximum contiguous sum is 30
Similar Reads
Maximum subarray sum in an array created after repeated concatenation | Set-2 Given an array arr[] consisting of N 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 = 2Output: 59Explanation:After concatenating the array tw
15+ min read
Maximum subarray sum in array formed by repeating the given array k times Given an integer k and an integer array arr[] of n elements, the task is to find the largest sub-array sum in the modified array (formed by repeating the given array k times). For example, if arr[] = {1, 2} and k = 3 then the modified array will be {1, 2, 1, 2, 1, 2}. Examples: Input: arr[] = {1, 2}
12 min read
Find the maximum sum after dividing array A into M Subarrays Given a sorted array A[] of size N and an integer M. You need to divide the array A[] into M non-empty consecutive subarray (1 ? M ? N) of any size such that each element is present in exactly one of the M-subarray. After dividing the array A[] into M subarrays you need to calculate the sum [max(i)
10 min read
Find Maximum Sum Strictly Increasing Subarray Given an array of positive integers. Find the maximum sum of strictly increasing subarrays. Note that this problem is different from maximum subarray sum and maximum sum increasing subsequence problems. Examples: Input : arr[] = {1, 2, 3, 2, 5, 1, 7}Output : 8Explanation : Some Strictly increasing s
7 min read
Maximum sum subarray after altering the array Given an array arr[] of size N. The task is to find the maximum subarray sum possible after performing the given operation at most once. In a single operation, you can choose any index i and either the subarray arr[0...i] or the subarray arr[i...N-1] can be reversed. Examples: Input: arr[] = {3, 4,
15+ min read