-
Notifications
You must be signed in to change notification settings - Fork 0
/
q7.cpp
74 lines (60 loc) · 1.55 KB
/
q7.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
int maximumProfit(int price[],n)
{
int buy = 0;
int sell = 1;
int temp = 0;
int MaxProfit = price[1] - price[0];
int BuyDay = price[0];
for(int i = 1; i < size; i++)
{
//compare current maximum difference with difference of each day's price with minimum
if (price[i] - BuyDay > MaxProfit)
{
MaxProfit = price[i] - BuyDay;
buy = temp;
sell = i;
}
//updates BuyDay if a price less than current min price is found
if (price[i] < BuyDay)
{
BuyDay = price[i];
temp = i;
}
}
if(MaxProfit < 1)
{
cout<<"\n\n No Maximum profit found!!";
}
else
{
cout<<"\n\nBuy Day: "<<buy+1<<",Sell Day: "<<sell+1;
}
cout<<"\n";
return MaxProfit;
}
/* Driver program to test above function */
int main()
{
int size;
cout << "Enter Number of consecutive days: ";
cin >> size;
int price[size];
cout<<"\nEnter Price per share for the stock \n";
for(int i=0;i < size;i++)
{
cout<<"Enter price [ "<<i+1<<" ] : ";
cin>>price[i];
}
int profit = maximumProfit(price,size);
if(profit > 0)
cout << "\nMaximum money earned " << profit;
cout<<endl;
return 0;
/*
Time Complexity:
line 17-29 runs n time that is number of days
therefore,T(n)=O(n)
*/
}