Skip to content

Commit

Permalink
Clean up content
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingh authored and sunsingh committed Aug 26, 2019
1 parent d3bee52 commit d6f65eb
Showing 1 changed file with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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};
// }



}

0 comments on commit d6f65eb

Please sign in to comment.