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
A simple solution is to create an array of size n*k, then run Kadane’s algorithm. Time complexity would be O(nk) and auxiliary space would be O(n*k)
A better solution is to run a loop on same array and use modular arithmetic to move back beginning after end of array.
C++
// C++ program to print largest contiguous // array sum when array is created after // concatenating a small array k times. #include<bits/stdc++.h> using namespace std; // Returns sum of maximum sum subarray created // after concatenating a[0..n-1] k times. int maxSubArraySumRepeated(int a[], int n, int k) { int max_so_far = INT_MIN, max_ending_here = 0; for (int i = 0; i < n*k; i++) { // This is where it differs from Kadane's // algorithm. We use modular arithmetic to // find next element. max_ending_here = max_ending_here + a[i%n]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } /*Driver program to test maxSubArraySum*/int main() { int a[] = {10, 20, -30, -1}; int n = sizeof(a)/sizeof(a[0]); int k = 3; cout << "Maximum contiguous sum is " << maxSubArraySumRepeated(a, n, k); return 0; } |
Java
// Java program to print largest contiguous // array sum when array is created after // concatenating a small array k times. import java.io.*; class GFG { // Returns sum of maximum sum // subarray created after // concatenating a[0..n-1] k times. static int maxSubArraySumRepeated(int a[], int n, int k) { int max_so_far = 0; int INT_MIN, max_ending_here=0; for (int i = 0; i < n*k; i++) { // This is where it differs from // Kadane's algorithm. We use modular // arithmetic to find next element. max_ending_here = max_ending_here + a[i % n]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } // Driver program to test maxSubArraySum public static void main (String[] args) { int a[] = {10, 20, -30, -1}; int n = a.length; int k = 3; System.out.println("Maximum contiguous sum is " + maxSubArraySumRepeated(a, n, k)); } } // This code is contributed by vt_m |
Python3
# Python program to print # largest contiguous # array sum when array # is created after # concatenating a small # array k times. # Returns sum of maximum # sum subarray created # after concatenating # a[0..n-1] k times. def maxSubArraySumRepeated(a, n, k): max_so_far = -2147483648 max_ending_here = 0 for i in range(n*k): # This is where it # differs from Kadane's # algorithm. We use # modular arithmetic to # find next element. max_ending_here = max_ending_here + a[i%n] if (max_so_far < max_ending_here): max_so_far = max_ending_here if (max_ending_here < 0): max_ending_here = 0 return max_so_far # Driver program # to test maxSubArraySum a = [10, 20, -30, -1] n = len(a) k = 3 print("Maximum contiguous sum is ", maxSubArraySumRepeated(a, n, k)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to print largest contiguous // array sum when array is created after // concatenating a small array k times. using System; class GFG { // Returns sum of maximum sum // subarray created after // concatenating a[0..n-1] k times. static int maxSubArraySumRepeated(int []a, int n, int k) { int max_so_far = 0; int max_ending_here=0; for (int i = 0; i < n * k; i++) { // This is where it differs from // Kadane's algorithm. We use modular // arithmetic to find next element. max_ending_here = max_ending_here + a[i % n]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } // Driver Code public static void Main () { int []a = {10, 20, -30, -1}; int n = a.Length; int k = 3; Console.Write("Maximum contiguous sum is " + maxSubArraySumRepeated(a, n, k)); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP program to print largest contiguous // array sum when array is created after // concatenating a small array k times. // Returns sum of maximum // sum subarray created // after concatenating // a[0..n-1] k times. function maxSubArraySumRepeated($a, $n, $k) { $INT_MIN=0; $max_so_far = $INT_MIN; $max_ending_here = 0; for ($i = 0; $i < $n*$k; $i++) { // This is where it differs // from Kadane's algorithm. // We use modular arithmetic // to find next element. $max_ending_here = $max_ending_here + $a[$i % $n]; if ($max_so_far < $max_ending_here) $max_so_far = $max_ending_here; if ($max_ending_here < 0) $max_ending_here = 0; } return $max_so_far; } // Driver Code $a = array(10, 20, -30, -1); $n = sizeof($a); $k = 3; echo "Maximum contiguous sum is " , maxSubArraySumRepeated($a, $n, $k); // This code is contributed by nitin mittal. ?> |
Output :
Maximum contiguous sum is 30
Can we use the repeating property of array to get a better solution ?
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.
Recommended Posts:
- Find maximum array sum after making all elements same with repeated subtraction
- Maximum height of the binary search tree created from the given array
- Minimum array size after repeated replacement of even sum pair with sum
- Maximize the subarray sum after multiplying all elements of any subarray with X
- First subarray having sum at least half the maximum sum of any subarray of size K
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Maximum sum subarray after altering the array
- Subarray of length K having concatenation of its elements divisible by X
- Subarray of length K whose concatenation forms a palindrome
- Size of array after repeated deletion of LIS
- Maximum length of subarray such that sum of the subarray is even
- Maximum Subarray Sum after inverting at most two elements
- Maximize the maximum subarray sum after removing atmost one element
- Subarray with largest sum after excluding its maximum element
- Rearrange array to obtain maximum possible value of concatenation of prefix GCDs
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nitin mittal

