-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaxSubArray.java
More file actions
executable file
·24 lines (20 loc) · 846 Bytes
/
Copy pathmaxSubArray.java
File metadata and controls
executable file
·24 lines (20 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
class maxSubArrayProd {
static int maxProduct(int[] nums) {
// if initialized to 1, then the program will fail for the test case where ans is 0.
int res=nums[0],curr_max=nums[0],curr_min=nums[0],temp;
System.out.println("curr_max"+" "+"curr_min");
for(int i=1;i<nums.length;i++){
temp=curr_max;
curr_max=Math.max(Math.max(nums[i]*curr_max,nums[i]*curr_min),nums[i]);
curr_min=Math.min(Math.min(nums[i]*temp,nums[i]*curr_min),nums[i]);
res=Math.max(curr_max,res);
System.out.println(curr_max+" \t\t "+curr_min);
}
return res;
}
public static void main(String[] args){
int arr[] = { 1, -2, -3, 0, 7, -8, -2 };
System.out.println("\nMaximum Sub array product is "+ maxProduct(arr));
}
}