SHARE
TWEET

Untitled

a guest Aug 24th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MaxSubArraySum {
  2.  
  3.     public static void main(String[] args) {
  4.         // TODO Auto-generated method stub
  5.         int[] a = {1, -3, 2, 1,-1};
  6.         int result = maxSumSubarray(a);
  7.         System.out.println(result);
  8.     }
  9.    
  10.     public static int maxSumSubarray(int[] A){
  11.         int n = A.length;
  12.         int local_max = 0;
  13.         int global_max = Integer.MIN_VALUE;
  14.          
  15.         for(int i = 0; i < n; i++){
  16.             int current = A[i];
  17.             local_max = Math.max(current, current + local_max);
  18.             if(local_max > global_max){
  19.                 global_max = local_max;
  20.             }
  21.         }
  22.         return global_max;
  23.     }
  24. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top