fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public int maxSubArray(int[] nums) {
  11.  
  12. int max_so_far = nums[0];
  13. int curr_max = nums[0];
  14.  
  15. for (int i = 1; i < nums.length; i++) {
  16. curr_max = Math.max(nums[i], nums[i] + curr_max);
  17. max_so_far = Math.max(curr_max, max_so_far);
  18. }
  19.  
  20. return max_so_far;
  21. }
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. // your code goes here
  25. Ideone x = new Ideone();
  26. int[] arr = {-2, -5, 6, -2, -3, 1, 5, -6};
  27.  
  28. System.out.println(x.maxSubArray(arr));
  29. }
  30. }
Success #stdin #stdout 0.06s 32472KB
stdin
Standard input is empty
stdout
7