(int arr[])
| 41 | |
| 42 | //Kadane's Algorithm |
| 43 | public static void maxSubarraySum3(int arr[]) { |
| 44 | int cs = 0; |
| 45 | int ms = 0; |
| 46 | |
| 47 | for(int i=0; i<arr.length; i++) { |
| 48 | cs = cs + arr[i]; |
| 49 | if(cs < 0) { |
| 50 | cs = 0; |
| 51 | } |
| 52 | ms = Math.max(ms, cs); |
| 53 | } |
| 54 | |
| 55 | System.out.println("max subarray sum is : " + ms); |
| 56 | } |
| 57 | public static void main(String args[]) { |
| 58 | int arr[] = {1, -2, 6, -1, 3}; |
| 59 | maxSubarraySum1(arr); |