MCPcopy Create free account
hub / github.com/careercup/ctci / getMaxSum

Method getMaxSum

java/Chapter 17/Question17_8/Question.java:5–17  ·  view source on GitHub ↗
(int[] a)

Source from the content-addressed store, hash-verified

3public class Question {
4
5 public static int getMaxSum(int[] a) {
6 int maxSum = 0;
7 int runningSum = 0;
8 for (int i = 0; i < a.length; i++) {
9 runningSum += a[i];
10 if (maxSum < runningSum) {
11 maxSum = runningSum;
12 } else if (runningSum < 0) {
13 runningSum = 0;
14 }
15 }
16 return maxSum;
17 }
18
19 public static void main(String[] args) {
20 int[] a = {2, -8, 3, -2, 4, -10};

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected