Array Question 18
Question 18. Kadane's Algorithm
Program
class Solution{ long maxSubarraySum(int arr[], int n){ long cursum = 0; long maxsum = Integer.MIN_VALUE; for(int i=0;i<n;i++){ cursum = cursum+arr[i]; if(cursum > maxsum){ maxsum = cursum; } if(cursum <0){ cursum = 0; } } return maxsum; } } |