Maximum subarray sum in an array created after repeated concatenation Last Updated : 02 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 = 2Output : 59After concatenating array twice, we get {-1, 10, 20, -1, 10, 20} which has maximum subarray sum as 59.Input : arr[] = {-1, -2, -3}, k = 3Output : -1Naive Approach - O(nk) Time and O(nk) SpaceA 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) SpaceAn 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 Comment More infoAdvertise with us Next Article Maximum subarray sum in an array created after repeated concatenation S SandeepC Follow Improve Article Tags : Interview Experiences Dynamic Programming DSA Arrays Experiences Modular Arithmetic subarray subarray-sum +4 More Practice Tags : ArraysDynamic ProgrammingModular Arithmetic 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 Maximum product of sum of two contiguous subarrays of an array Given an array arr[] of N positive integers, the task is to split the array into two contiguous subarrays such that the product of the sum of two contiguous subarrays is maximum. Examples: Input: arr[] = {4, 10, 1, 7, 2, 9} Output: 270 All possible partitions and their product of sum are: {4} and {1 10 min read Maximum sum obtained by dividing Array into several subarrays as per given conditions Given an array arr[] of size N, the task is to calculate the maximum sum that can be obtained by dividing the array into several subarrays(possibly one), where each subarray starting at index i and ending at index j (j>=i) contributes arr[j]-arr[i] to the sum. Examples: Input: arr[]= {1, 5, 3}, N 5 min read Sum of maximum of all subarrays | Divide and Conquer Given an array arr[] of length N, the task is to find the sum of the maximum elements of every possible sub-array of the array.Examples: Input : arr[] = {1, 3, 1, 7} Output : 42 Max of all sub-arrays: {1} - 1 {1, 3} - 3 {1, 3, 1} - 3 {1, 3, 1, 7} - 7 {3} - 3 {3, 1} - 3 {3, 1, 7} - 7 {1} - 1 {1, 7} - 12 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 Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime Given an array arr[] of size N, the task is to find the maximum sum of a subsequence formed by concatenating disjoint subarrays whose lengths are prime numbers. Examples: Input: arr[] = {10, 10, 7, 10, 10, 10}Output: 50Explanation:Subsequence with maximum sum is obtained by concatenating following t 14 min read Like