How to solve this problem?
Hello @kunaldude04,
Here, you have to considered the given array as a circular sequence.
example:
for the array : {-1,2,8,-5,6}
the sub-sequences are:
{-1}=-1
{-1,2} = 1(sum)
{-1,2,8} = 9
{-1,2,8,-5}= 4
{-1,2,8,-5,6} = 10
{2]=2
{2,8} = 10
{2,8,-5}=5
…
…
{6}=6
{6,-1}=5 (coz array is considered circular)
{6,-1,2} =7
…
print the maximum.
Algorithm:
It’s the same as the maximum subarray sum.
You have to solve this question for in two parts:
-
maximum sum possible in a linear manner, use kadane’s algorithm to compute this (say max_linear)
-
Find the maximum sum possible in a circular manner:
…First negate all the elements of the array. i.e. covert negative nos. to positive and vice-a-versa example: 1- to 1 and 5 to -5.
…Now, apply kadane’s algorithm on this modified array and find a maximum value which the minimum value for actual array(say min2).
…At last, add this min2 to the sum of all the elements of the actual array (say max2). -
Compare max1 and max2.
-
Print the maximum of both.
Hope, this would help.
Give a like if you are satisfied.