Skip to content

Commit 197e68f

Browse files
committed
Maximum Circular Subarray sum
1 parent d18bcda commit 197e68f

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
3+
PROBLEM STATEMENT: Given an array of n integers, you need to find the maximum subarray sum, considering the fact that the array if circular.
4+
In other words of if the array if arr[] then next value for the i-th index is i+1 if i <n else if i==n then next index i 0
5+
6+
SAMPLE TEST CASES:
7+
8+
INPUT
9+
[-3, 3, 5, -2, 10, -30]
10+
OUTPUT
11+
16
12+
EXPLANATION {3+5-2+10} = 16, no other subarray will give better result
13+
14+
15+
INPUT
16+
[10, -3, -2, 4, -1]
17+
OUTPUT
18+
13
19+
EXPLANATION {4-1+10}=13 here 4, -1, 10 is considered. P.S remember the array is circular.
20+
21+
*/
22+
23+
24+
// SOLUTION:
25+
26+
27+
// Implementation of kadane's algo
28+
var maxSubAraySum = function(arr){
29+
var sumSF = 0, res=0;
30+
for(var i=0; i<arr.length; i++){
31+
sumSF=Math.max(0, sumSF+arr[i]);
32+
res = Math.max(res, sumSF);
33+
}
34+
35+
return res;
36+
}
37+
var maxSubarraySumCircular = function(A) {
38+
if(A.length==0) return 0;
39+
const maxi = Math.max(...A);
40+
if(maxi<=0) return maxi;
41+
42+
const val1 = maxSubAraySum(A);
43+
var totalSum=0;
44+
for(var i=0; i<A.length; i++){
45+
totalSum += A[i];
46+
A[i]=-1*A[i];
47+
}
48+
const val2 = totalSum + maxSubAraySum(A);
49+
return Math.max(val1, val2);
50+
51+
};
52+
53+
/*
54+
Solution Explanation:
55+
56+
First learn about Kadane's Algo, https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane's_algorithm
57+
We will be modifying the Kadane's Algo to find the maximum circular subarray sum. (mcss)
58+
59+
Now there are two cases:
60+
1. The optimal subarray will be simply present in the array, without the requirement of wrapping around the ends. Example Sample test case 1
61+
62+
63+
- - - - [- - - - - - - - -] - - - - -
64+
|<-optimal array->|
65+
66+
2. Now the optimal subarray will be wrapper around the edges.
67+
|- - - - - ]- - - - - - - [- - - - - - |
68+
|left seg->| |<- right seg|
69+
70+
That is there will be two segments one from left another from right.
71+
72+
73+
For the first case we will simply do Kadane. And store the result in val1,
74+
75+
For the second case if we look closely we will notice this relation
76+
totalArray = leftSeg + MidLeftOutSeg + rightSeg
77+
78+
we need to find the leftSeg + rightSeg
79+
80+
or, leftSeg + rightSeg = totalArray - MidLeftOutSeg
81+
82+
83+
So basically we need to minimize the absolute value of MidLeftOutSeg. Do we need to write another function for finding the minimum value of MidLeftOutSeg ? NO!! we will simply invert the values of the array and then again apply Kadane's Algo to get the max "inverted" sum, which is nothing but the minimum sum.
84+
85+
And then we will add( why add ? because we once inverted the signs and minus of minus makes plus. ) So we will val2.
86+
87+
Now finally we will return the max value of val1 and val2
88+
89+
*/

0 commit comments

Comments
 (0)