-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpink floyd
54 lines (47 loc) · 1.19 KB
/
pink floyd
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
Pink is sad because of some reasons, he wants to cheer up by listening to some songs
from his favorite band, Pink Floyd.
There are N records and Pink will be happy if he listens to them in the ascending
order, i.e., first the song No. 1, then No.2 and so on (He has to listen to all the N songs
to become Happy).
Pink is delivered his records in some given order, he can either add the record to the
Playlist in the delivered order or put some on another table. After being put on the
table only the topmost record can be added to the playlist at any time.
Print whether Pink will be sad or happy after the delivery of the records.
Input Format
N - Number of records followed by
N numbers- order of records.
Output Format
Print "Happy" if the playlist has songs from 1 to N in order else "Sad".
Constraints
1<=N<=10^5
The array consists of 1-N distinct numbers.
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;cin>>n;
ll a[n+1];
for(ll i=1;i<=n;i++)
cin>>a[i];
ll c=1;
stack<ll> s;
for(ll i=1;i<=n;i++)
{
s.push(a[i]);
while((!s.empty()) && (c==s.top()))
{
c++;
s.pop();
}
}
if(!s.empty())
cout<<"Sad"<<endl;
else
cout<<"Happy"<<endl;
}
Input:
5
1 2 4 3 5
Output:
Happy