-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestSubarray.java
62 lines (57 loc) · 1.6 KB
/
LargestSubarray.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
59
60
61
62
class LargestSubarray {
public static void printSubarray(int arr[]){
for(int i=0;i<arr.length;i++){
for(int j=i;j<arr.length;j++){
for(int k=i;k<=j;k++)
System.out.print(arr[k]+" ");
System.out.println();
}
System.out.println();
}
}
public static int maxSubarraySum(int arr[]){
int currSum=0,maxSum=Integer.MIN_VALUE;
//Bruteforce approach
for(int i=0;i<arr.length;i++){
for(int j=i;j<arr.length;j++){
for(int k=i;k<=j;k++)
currSum+=arr[k];
if(currSum>maxSum)
maxSum=currSum;
}
}
return maxSum;
}
public static int maxSubarraySum2(int arr[]){
//Prefix approach -better
int prefix[]=new int[arr.length];
prefix[0]=arr[0];
int currSum=0,maxSum=Integer.MIN_VALUE;
for(int i=1;i<arr.length;i++)
prefix[i]=prefix[i-1]+arr[i];
for(int i=0;i<arr.length;i++){
for(int j=i;j<arr.length;j++){
currSum=i==0?prefix[j]:prefix[j]-prefix[i-1];
if(currSum>maxSum)
maxSum=currSum;
}
}
return maxSum;
}
public static int kandanes(int arr[]){
//Best approach - kadanes algo
int currSum=0,maxSum=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
currSum+=arr[i];
if(currSum<0)
currSum=0;
if(currSum>maxSum)
maxSum=currSum;
}
return maxSum;
}
public static void main(String args[]){
int arr[]={-1,-2,1,5,-1,6,-6};
System.out.println("Max subarray sum="+kandanes(arr));
}
}