MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxAbsoluteSum

Method maxAbsoluteSum

MaximumAbsoluteSumOfAnySubarray.java:2–22  ·  view source on GitHub ↗
(int[] nums)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int maxAbsoluteSum(int[] nums) {
3 int maxSum = Integer.MIN_VALUE;
4 int minSum = Integer.MAX_VALUE;
5 int curPSum=0;
6 int curNSum=0;
7 for(int num : nums){
8 // pos
9 curPSum += num;
10 maxSum = Math.max(maxSum,curPSum);
11 if(curPSum < 0){
12 curPSum = 0;
13 }
14 // neg
15 curNSum += num;
16 minSum = Math.min(minSum,curNSum);
17 if(curNSum > 0){
18 curNSum = 0;
19 }
20 }
21 return Math.max(maxSum, Math.abs(minSum));
22 }
23}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected