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: A simple solution is to create an array of size n*k, then run Kadane’s algorithm.
- Time Complexity: O(nk)
- Auxiliary Space: O(n*k)
Efficient Approach: A better 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;
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++)
{
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;
}
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
import java.io.*;
class GFG {
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++)
{
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;
}
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));
}
}
|
Python3
def maxSubArraySumRepeated(a, n, k):
max_so_far = - 2147483648
max_ending_here = 0
for i in range (n * k):
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
a = [ 10 , 20 , - 30 , - 1 ]
n = len (a)
k = 3
print ( "Maximum contiguous sum is " ,
maxSubArraySumRepeated(a, n, k))
|
C#
using System;
class GFG {
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++)
{
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;
}
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));
}
}
|
PHP
<?php
function maxSubArraySumRepeated( $a , $n , $k )
{
$INT_MIN =0;
$max_so_far = $INT_MIN ; $max_ending_here = 0;
for ( $i = 0; $i < $n * $k ; $i ++)
{
$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 ;
}
$a = array (10, 20, -30, -1);
$n = sizeof( $a );
$k = 3;
echo "Maximum contiguous sum is "
, maxSubArraySumRepeated( $a , $n , $k );
?>
|
Javascript
<script>
function maxSubArraySumRepeated(a, n, k)
{
let max_so_far = 0;
let INT_MIN, max_ending_here=0;
for (let i = 0; i < n*k; i++)
{
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;
}
let a = [10, 20, -30, -1];
let n = a.length;
let k = 3;
document.write( "Maximum contiguous sum is "
+ maxSubArraySumRepeated(a, n, k));
</script>
|
Output
Maximum contiguous sum is 30
Time Complexity: O(n*k)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!