-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stocks.cpp
55 lines (47 loc) · 862 Bytes
/
Stocks.cpp
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
#include <iostream>
#include <climits>
using namespace std;
// brute-force approach
int profitStocks(int arr[], int n)
{
int profit = 0;
int mxProfit = INT_MIN;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
profit = arr[j] - arr[i];
mxProfit = max(mxProfit, profit);
// cout<<mxProfit<<" ";
}
}
if(mxProfit<0)
{
return 0;
}
return mxProfit;
}
int proft(int arr[], int n)
{
int mn = INT_MAX;
int prof = 0;
for(int i=0; i<n; i++)
{
mn = min(mn,arr[i]);
prof = max(prof,arr[i]-mn);
}
return prof;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<proft(arr,n);
// cout << profitStocks(arr, n);
return 0;
}