-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstpar.cpp
More file actions
49 lines (46 loc) · 927 Bytes
/
stpar.cpp
File metadata and controls
49 lines (46 loc) · 927 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
43
44
45
46
47
48
49
#include <iostream>
#include <stack>
int n;
int a[1000];
int main()
{
/* code */
std::cin >> n;
for (size_t i = 0; i < n; i++)
{
std::cin >> a[i];
}
std::stack<int> stack;
int tracker = 1; // since we are looking for the next elemetn: 1 (there's no 0)
for (size_t i = 0; i < n; i++)
{
while (stack.empty() == false && stack.top() == tracker)
{
tracker++;
stack.pop();
}
if (a[i] != tracker)
{
stack.push(a[i]);
}
else
{
tracker++;
}
}
// after, processing the whole arr, clear stack
while (stack.empty() == false && stack.top() == tracker)
{
tracker++;
stack.pop();
}
if (tracker == n + 1)
{
std::cout << "yes" << std::endl;
}
else
{
std::cout << "no" << std::endl;
}
return 0;
}