human
Maximum subarray sum in an array created after repeated concatenation | Set | Function to find contiguous subarray with maximum sum if array is repeated K times.
Store the sum of the array arr [ ].
Traverse the array and find sum.
Store the answer.
If K = 1.
Apply Kadane algorithm to find sum.
Return the answer.
Stores the twice repeated array.
Traverse the range [ 0 , 2 * N ].
Stores the maximum suffix sum.
Stores the maximum prefix sum.
Apply Kadane algorithm for 2 repetition of the array.
If the sum of the array is greater than 0.
Return the answer.
Driver Code.
Given Input.
Function Call"
Solution in Python:
assistant
#Here is code to maximum subarray sum in an array created after repeated concatenation and the other steps required.
 def maxSubArraySumRepeated ( arr , N , K ) :
	sum = 0
	for i in range ( N ) :
		sum += arr [ i ]
	curr = arr [ 0 ]
	ans = arr [ 0 ]
	if ( K == 1 ) :
		for i in range ( 1 , N , 1 ) :
			curr = max ( arr [ i ] , curr + arr [ i ] )
			ans = max ( ans , curr )
		return ans
	V = [ ]
	for i in range ( 2 * N ) :
		V . append ( arr [ i % N ] )
	maxSuf = V [ 0 ]
	maxPref = V [ 2 * N - 1 ]
	curr = V [ 0 ]
	for i in range ( 1 , 2 * N , 1 ) :
		curr += V [ i ]
		maxPref = max ( maxPref , curr )
	curr = V [ 2 * N - 1 ]
	i = 2 * N - 2
	while ( i >= 0 ) :
		curr += V [ i ]
		maxSuf = max ( maxSuf , curr )
		i -= 1
	curr = V [ 0 ]
	for i in range ( 1 , 2 * N , 1 ) :
		curr = max ( V [ i ] , curr + V [ i ] )
		ans = max ( ans , curr )
	if ( sum > 0 ) :
		temp = sum * ( K - 2 )
		ans = max ( ans , max ( temp + maxPref , temp + maxSuf ) )
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ 10 , 20 , - 30 , - 1 , 40 ]
	N = len ( arr )
	K = 10
	print ( maxSubArraySumRepeated ( arr , N , K ) )