forked from YashPatel1311/Finding-Prime-Numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximumSum.java
58 lines (38 loc) · 1.01 KB
/
MaximumSum.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
52
53
54
55
56
57
58
import java.util.Arrays;
class MaximumSum{
int[] Maximum(int[] arr){
int max_sum=-1000;
int sum=0;
int start=0;
int end=arr.length;
for(int i=0; i<arr.length;i++)
{
for(int j=arr.length-1;j>=i;j--){
sum=0;
for(int k=i;k<=j;k++){
sum=sum+arr[k];
}
if(max_sum<sum){
max_sum=sum;
start=i;
end=j;
}
}
}
int[] result=new int[end-start+1];
int count=0;
int x=start;
while(count<result.length && x<=end){
result[count]=arr[x];
count++;
x++;
}
return result;
}
public static void main(String[]args){
int[] arr={-2,-5,6,-2,-3,1,5,-6};
MaximumSum r1=new MaximumSum();
int [] result =r1.Maximum(arr);
System.out.println(Arrays.toString(result));
}
}