View Code of Problem 19

import java.util.Scanner;

public class Main {
    public static void main(String[] args){

        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {

            Integer n = in.nextInt();
            if(n==0) return;
            int[] A=new int[n];
            for(int i=0;i<n;i++){
                A[i]=in.nextInt();
            }
            System.out.println(getMaxSum(A,n));
        }
    }

    public static int  getMaxSum(int[] A, int n) {
        // write code here
        if(A==null || n==0) return -1;
        if(n==1) return A[0];
        int max=A[0];
        int sum=max;
        for(int i=1;i<n;i++){
            if(sum<0) {
                sum=A[i];
            }else{
                sum+=A[i];
            }
            max=max<sum?sum:max;

        }
        return max;
    }
    
}

Double click to view unformatted code.


Back to problem 19