-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_Diff_array.cpp
More file actions
42 lines (37 loc) · 893 Bytes
/
Max_Diff_array.cpp
File metadata and controls
42 lines (37 loc) · 893 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*maximum difference between two elements
*/
#include <bits/stdc++.h>
using namespace std;
int maxProfit(int prices[], int N)
{
int n = N;
int cost = 0;
int maxCost = 0;
if (n == 0)
{
return 0;
}
int min_price = prices[0];
for(int i = 0; i < n; i++)
{
// Now compare first element with all
// the element of array and find the
// minimum element
min_price = min(min_price, prices[i]);
// Since min_price is smallest element of the
// array so subtract with every element of the
// array and return the maxCost
cost = prices[i] - min_price;
maxCost = max(maxCost, cost);
}
return maxCost;
}
int main()
{
// Stock prices on consecutive days
int prices[] = { 1, 2, 90, 10, 110};
int N = sizeof(prices) / sizeof(prices[0]);
cout << maxProfit(prices, N);
return 0;
}
// This code is contributed by divyeshrabadiya07