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 modified array will be {1, 2, 1, 2, 1, 2}.
Examples:
Input: arr[] = {1, 2}, k = 3
Output: 9
Modified array will be {1, 2, 1, 2, 1, 2}
And the maximum sub-array sum will be 1 + 2 + 1 + 2 + 1 + 2 = 9Input: arr[] = {1, -2, 1}, k = 5
Output: 2
A simple solution is to create an array of size n * k, then run Kadane’s algorithm to find the maximum sub-array sum. Time complexity would be O(n * k) with auxiliary space O(n * k).
A better solution is to calculate the sum of the array arr[] and store it in sum.
- If sum < 0 then calculate the maximum sub-array sum of an array formed by concatenating the the array two times irrespective of the K. For example, take arr[] = {1, -4, 1} and k = 5. The sum of the array is less than 0. So, the maximum sub-array sum of the array can be found after concatenating the array two times only irrespective of the value of K i.e. b[] = {1, -4, 1, 1, -4, 1} and the maximum sub-array sum = 1 + 1 = 2
- If sum > 0 then maximum sub-array will include the maximum sum as calculated in the previous step (where the array was concatenated twice) + the rest (k – 2) repetitions of the array can also be included as their sum is greater than 0 that will contribute to the maximum sum.
.
Below is the implementation of the above approach:
Java
// Java implementation of the approach public class GFG { // Function to concatenate array static void arrayConcatenate( int arr[], int b[], int k) { // Array b will be formed by concatenation // array a exactly k times int j = 0 ; while (k > 0 ) { for ( int i = 0 ; i < arr.length; i++) { b[j++] = arr[i]; } k--; } } // Function to return the maximum // subarray sum of arr[] static int maxSubArrSum( int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0 ; for ( int i = 0 ; i < size; i++) { max_ending_here = max_ending_here + a[i]; 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; } // Function to return the maximum sub-array // sum of the modified array static long maxSubKSum( int arr[], int k) { int arrSum = 0 ; long maxSubArrSum = 0 ; int b[] = new int [( 2 * arr.length)]; // Concatenating the array 2 times arrayConcatenate(arr, b, 2 ); // Finding the sum of the array for ( int i = 0 ; i < arr.length; i++) arrSum += arr[i]; // If sum is less than zero if (arrSum < 0 ) maxSubArrSum = maxSubArrSum(b); // If sum is greater than zero else maxSubArrSum = maxSubArrSum(b) + (k - 2 ) * arrSum; return maxSubArrSum; } // Driver code public static void main(String[] args) { int arr[] = { 1 , - 2 , 1 }; int k = 5 ; System.out.println(maxSubKSum(arr, k)); } } |
Below is the implementation of the above approach:
Python
# Python approach to this problem # A python module where element # are added to list k times def MaxsumArrKtimes(c, ktimes): # Store element in list d k times d = c * ktimes # two variable which can keep # track of maximum sum seen # so far and maximum sum ended. maxsofar = - 99999 maxending = 0 for i in d: maxending = maxending + i if maxsofar < maxending: maxsofar = maxending if maxending < 0 : maxending = 0 return maxsofar # Get the Maximum sum of element print (MaxsumArrKtimes([ 1 , - 2 , 1 ], 5 )) # This code is contributed by AnupGaurav. |
C#
// C# implementation of the approach using System; class GFG { // Function to concatenate array static void arrayConcatenate( int []arr, int []b, int k) { // Array b will be formed by concatenation // array a exactly k times int j = 0; while (k > 0) { for ( int i = 0; i < arr.Length; i++) { b[j++] = arr[i]; } k--; } } // Function to return the maximum // subarray sum of arr[] static int maxSubArrSum( int []a) { int size = a.Length; int max_so_far = int .MinValue, max_ending_here = 0; for ( int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; 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; } // Function to return the maximum sub-array // sum of the modified array static long maxSubKSum( int []arr, int k) { int arrSum = 0; long maxSubArrsum = 0; int []b = new int [(2 * arr.Length)]; // Concatenating the array 2 times arrayConcatenate(arr, b, 2); // Finding the sum of the array for ( int i = 0; i < arr.Length; i++) arrSum += arr[i]; // If sum is less than zero if (arrSum < 0) maxSubArrsum = maxSubArrSum(b); // If sum is greater than zero else maxSubArrsum = maxSubArrSum(b) + (k - 2) * arrSum; return maxSubArrsum; } // Driver code public static void Main() { int []arr = { 1, -2, 1 }; int k = 5; Console.WriteLine(maxSubKSum(arr, k)); } } // This code is contributed by Ryuga |
PHP
<?php // PHP implementation of the approach // Function to concatenate array function arrayConcatenate(& $arr , & $b , $k ) { // Array b will be formed by concatenation // array a exactly k times $j = 0; while ( $k > 0) { for ( $i = 0; $i < sizeof( $arr ); $i ++) { $b [ $j ++] = $arr [ $i ]; } $k --; } } // Function to return the maximum // subarray sum of arr[] function maxSubArrSum(& $a ) { $size = sizeof( $a ); $max_so_far = 0; $max_ending_here = 0; for ( $i = 0; $i < $size ; $i ++) { $max_ending_here = $max_ending_here + $a [ $i ]; 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 ; } // Function to return the maximum sub-array // sum of the modified array function maxSubKSum(& $arr , $k ) { $arrSum = 0; $maxSubArrSum = 0; $b = array_fill (0,(2 * sizeof( $arr )),NULL); // Concatenating the array 2 times arrayConcatenate( $arr , $b , 2); // Finding the sum of the array for ( $i = 0; $i < sizeof( $arr ); $i ++) $arrSum += $arr [ $i ]; // If sum is less than zero if ( $arrSum < 0) $maxSubArrSum = maxSubArrSum( $b ); // If sum is greater than zero else $maxSubArrSum = maxSubArrSum( $b ) + ( $k - 2) * $arrSum ; return $maxSubArrSum ; } // Driver code $arr = array (1, -2, 1 ); $k = 5; echo maxSubKSum( $arr , $k ); // This code is contributed by Ita_c. ?> |
2
Recommended Posts:
- Sum of all elements repeating 'k' times in an array
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Maximum subarray sum in an array created after repeated concatenation
- Sort an array where a subarray of a sorted array is in reverse order
- Check whether an Array is Subarray of another Array
- k-th distinct (or non-repeating) element in an array.
- Find the two repeating elements in a given array
- Product of non-repeating (distinct) elements in an Array
- Find sum of non-repeating (distinct) elements in an array
- Find the first repeating element in an array of integers
- Find any one of the multiple repeating elements in read only array
- Find the only repeating element in a sorted array of size n
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Check whether Arithmetic Progression can be formed from the given array
- Minimum sum of two numbers formed from digits of an array
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.