-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 129.java
More file actions
33 lines (23 loc) · 764 Bytes
/
Day 129.java
File metadata and controls
33 lines (23 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public int maxCircularSum(int arr[]) {
int n = arr.length;
int maxEndingHere = arr[0];
int maxSoFar = arr[0];
int minEndingHere = arr[0];
int minSoFar = arr[0];
int totalSum = arr[0];
for (int i = 1; i < n; i++) {
int x = arr[i];
maxEndingHere = Math.max(x, maxEndingHere + x);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
minEndingHere = Math.min(x, minEndingHere + x);
minSoFar = Math.min(minSoFar, minEndingHere);
totalSum += x;
}
if (maxSoFar < 0) {
return maxSoFar;
}
int maxWrap = totalSum - minSoFar;
return Math.max(maxSoFar, maxWrap);
}
}