From d6f65eb91efec0d4463a838efc817d938298c371 Mon Sep 17 00:00:00 2001 From: sunsingh Date: Mon, 26 Aug 2019 16:22:13 +0530 Subject: [PATCH] Clean up content --- .../programs/array/SmallestSubArraySum.java | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/concept/core/src_extra/Datastructure/src/com/datastructure/programs/array/SmallestSubArraySum.java b/concept/core/src_extra/Datastructure/src/com/datastructure/programs/array/SmallestSubArraySum.java index 3f2a96d..2b76283 100644 --- a/concept/core/src_extra/Datastructure/src/com/datastructure/programs/array/SmallestSubArraySum.java +++ b/concept/core/src_extra/Datastructure/src/com/datastructure/programs/array/SmallestSubArraySum.java @@ -5,52 +5,52 @@ * @author sunil */ public class SmallestSubArraySum { - + public static void main(String[] args) { int[] arr = {3, -4, 2, -3, -1, 7, -5}; //[m x n] System.out.println("Min " + minSum(arr)); - -// int[]index = minSumBracket(arr); -// System.out.println("startIndex " + index[0] + " endIndex " + index[1]); + + int[] index = minSumBracket(arr); + System.out.println("startIndex " + index[0] + " endIndex " + index[1]); } - - public static int minSum(int[] arr){ + + public static int minSum(int[] arr) { int min = Integer.MAX_VALUE; int minSoFar = Integer.MAX_VALUE; - + for (int i = 0; i < arr.length; i++) { - if(min > 0){ + if (min > 0) { min = arr[i]; - }else{ + } else { min += arr[i]; minSoFar = Math.min(min, minSoFar); } } - return minSoFar; - + return minSoFar; + + } + + public static int[] minSumBracket(int[] arr) { + int min = Integer.MAX_VALUE; + int minSoFar = Integer.MAX_VALUE; + int startIndex = 0; + int endIndex = 0; + for (int i = 0; i < arr.length; i++) { + if (min > 0) { + min = arr[i]; + if (min < minSoFar) { + startIndex = i; + } + } else { + min += arr[i]; + if (min < minSoFar) { + endIndex = i; + } + } + minSoFar = Math.min(min, minSoFar); + } + + return new int[]{startIndex, endIndex}; } - -// public static int[] minSumBracket(int[] arr){ -// int min = Integer.MAX_VALUE; -// int minSoFar = Integer.MAX_VALUE; -// int startIndex = -1; -// int endIndex = -1; -// for (int i = 0; i < arr.length; i++) { -// if(min > 0 ){ -// min = arr[i]; -// }else{ -// min += arr[i]; -// endIndex = i; -// } -// if(min < minSoFar){ -// startIndex = i; -// } -// minSoFar = Math.min(min, minSoFar); -// -// } -// -// return new int[]{startIndex,endIndex}; -// } - - + }