daily pastebin goal
87%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MaxSubArrayUsingKadane {
  2.      int[] maxSubArray(int[] A) {
  3.         if(A.length == 0) return null;
  4.        
  5.         int max = A[0], sum = A[0];
  6.         int high = 0, low = 0;
  7.         for(int i = 1; i < A.length; i++) {
  8.             if(sum+A[i] <= A[i]) {
  9.                 sum = A[i];
  10.                 low = i;
  11.                 System.out.println(low);
  12.             } else {
  13.                 sum = sum+A[i];
  14.             }
  15.            
  16.             if(max <= sum) {
  17.                 max = sum;
  18.                 high = i;
  19.             }
  20.         }
  21.         //This check is necessary when all inputs are less <= 0
  22.         if(high < low) {
  23.             low = high;
  24.         }
  25.         int[] ans = {low, high, max};
  26.         return ans;
  27.     }
  28.    
  29. }
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