-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSumTest.java
51 lines (37 loc) · 1.04 KB
/
MaxSumTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class MaxSumTest{
public static void findMaxSumSequence (int inputArray[])
{
if (inputArray == null || inputArray.length == 0)
throw new IllegalArgumentException("Array is empty");
int size = inputArray.length;
int maxSum = inputArray[0];
int maxStartIndex = 0;
int maxEndIndex = 0;
int curSum = inputArray[0];
int curStartIndex = 0;
for (int i = 1; i < size; i++)
{
if (curSum < 0)
{
curSum = 0;
curStartIndex = i;
}
curSum = curSum + inputArray[i];
if (curSum > maxSum)
{
maxSum = curSum;
maxStartIndex = curStartIndex;
maxEndIndex = i;
}
}
//System.out.print(inputArray[i+maxStartIndex]+ " ");
System.out.println("start index is : ' maxStartIndex);
System.out.println("end index is : ' maxEndIndex);
System.out.println("sum is = " + maxSum);
}
public static void main(String args[])
{
int a[]={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};
findMaxSumSequence(a);
}
}