MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxProduct

Method maxProduct

MaximumProductSubarray.java:3–25  ·  view source on GitHub ↗
(int[] arr, int n)

Source from the content-addressed store, hash-verified

1class Solution {
2 // Function to find maximum product subarray
3 long maxProduct(int[] arr, int n) {
4 // code here
5 long max=arr[0];
6 long min=arr[0];
7 long res=arr[0];
8 for(int i=1;i<n;i++)
9 {
10 if(arr[i]==0)
11 {
12 max=1;
13 min=1;
14 }
15 long temp1=arr[i]*max;
16 long temp2=arr[i]*min;
17 max=Math.max(temp1,temp2);
18 max=Math.max(max,arr[i]);
19 min=Math.min(temp1,temp2);
20 min=Math.min(min,arr[i]);
21 res=Math.max(res,max);
22 }
23 return res;
24
25 }
26}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected