Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package dynamicProgramming;
  2.  
  3. public class MaxSubArraySum {
  4.    
  5.     public static void main(String []args){
  6.         int [] test = {1,2,-3,-4,-5,6,3,-10};
  7.         System.out.println(maxSum(test));
  8.        
  9.     }
  10.  
  11.     public static int maxSum(int [] array){
  12.         int endSofar =Integer.MIN_VALUE;
  13.         int endHere = Integer.MIN_VALUE;
  14.         for(int i =0 ;i < array.length ;i++){
  15.             endHere = Math.max(0, endHere+array[i]);
  16.             endSofar = Math.max(endSofar, endHere);
  17.         }
  18.         return endSofar;
  19.     }
  20. }