Skip to content

Instantly share code, notes, and snippets.

@criskgl
Created August 12, 2019 11:02
Show Gist options
  • Save criskgl/e7e072de6d28f553080e9819b24fe15e to your computer and use it in GitHub Desktop.
Save criskgl/e7e072de6d28f553080e9819b24fe15e to your computer and use it in GitHub Desktop.
public class MinSubArraySum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1, 3, -2, -1,1};
int result = minSumSubarray(a);
System.out.println(result);
}
public static int minSumSubarray(int[] A){
int n = A.length;
int local_min = 0;
int global_min = Integer.MAX_VALUE;
for(int i = 0; i < n; i++){
int current = A[i];
local_min = Math.min(current, current + local_min);
if(local_min < global_min){
global_min = local_min;
}
}
return global_min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment