-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Maximum_Product_Subarray.c
88 lines (67 loc) · 1.48 KB
/
Maximum_Product_Subarray.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Find the maximum product subarray of a given array */
#include<stdio.h>
/* Function for finding minimum of two numbers */
int min(int p, int q)
{
if (p < q)
return p;
else
return q;
}
/* Function for finding maximum of two numbers */
int max(int p, int q)
{
if (p > q)
return p;
else
return q;
}
/* Function for finding maximum product subarray */
int solve()
{
/* Input size of an array */
int n;
scanf("%d", &n);
/* Declear an array*/
int a[n];
/* Input values in an array */
for (int i = 0; i < n; i++)
{
scanf("%d" , &a[i]);
}
int maxend = 0, minend = 0;
int maxupto = 0;
/* traverse the given array */
for (int i = 0; i < n; i++)
{
int temp = maxend;
/* Update the maximum product */
maxend = max(a[i], max(a[i] * maxend, a[i] * minend));
/* Update the minimum product */
minend = min(a[i], min(a[i] * temp, a[i] * minend));
maxupto = max(maxupto, maxend);
}
/* Return the maximum product */
return maxupto;
}
int main()
{
int k = solve();
printf("The maximum product of a subarray is %d", k);
return 0;
}
/*
Test cases :
Input 1 :
8
-4 9 -7 0 -15 6 2 -3
Output 1 :
The maximum product of a subarray is 540
Input 2 :
5
6 -3 -10 0 2
Output 2 :
The maximum product of a subarray is 180
Time complexity: O(n)
Space Complexity: O(1)
*/