Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement