-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_StockBuyAndSell.cpp
108 lines (93 loc) · 2.13 KB
/
GFG_StockBuyAndSell.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
https://practice.geeksforgeeks.org/problems/stock-buy-and-sell-1587115621/1#
Stock Buy and Sell
*/
// { Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
//Function to find the days of buying and selling stock for max profit.
vector<vector<int> > stockBuySell(vector<int> A, int n){
vector<vector<int>> ans;
int min_buy = 0;
int max_pft = 0, diff=0;
bool flag = false;
int i=0;
int j=1;
for(int k=1; k<A.size(); k++)
{
if(A[k] > A[k-1])
ans.push_back({k-1, k});
}
// while(j<A.size())
// {
// if(A[j] > A[i])
// {
// j++;
// flag = true;
// }
// else
// {
// diff = A[j] - A[i];
// if(flag)
// {
// flag=0;
// ans.push_back({i, j-1});
// }
// i=j;
// j++;
// }
// }
// if(flag)
// {
// flag=0;
// ans.push_back({i, j-1});
// }
// for(auto x: ans)
// {
// cout<<x[0]<<" "<<x[1]<<endl;
// }
return ans;
}//
};
// { Driver Code Starts.
int check(vector<vector<int>> ans, vector<int> A, int p)
{
int c = 0;
for(int i=0; i<ans.size(); i++)
c += A[ans[i][1]]-A[ans[i][0]];
return (c==p) ? 1 : 0;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> A(n);
for (int i=0; i<n; ++i){
cin>>A[i];
}
Solution ob;
vector<vector<int> > ans = ob.stockBuySell(A, n);
int p = 0;
for(int i=0; i<n-1; i++)
{
int x = A[i+1]-A[i];
if(x>0)
p += x;
}
if(ans.size()==0)
cout<<"No Profit";
else{
cout<<check(ans,A,p);
}cout<<endl;
}
return 0;
}
// } Driver Code Ends