-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTask 06.cpp
More file actions
64 lines (56 loc) · 1.43 KB
/
Task 06.cpp
File metadata and controls
64 lines (56 loc) · 1.43 KB
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
#include<iostream>
#include<stack>
using namespace std;
/*
For any element, if there are elements after it in the stack that are smaller,
any elements that come before it cannot have those elements as results. So,
the smaller elements can be popped.
*/
void NGE(int arr[], int j)
{
stack<int> a;
int results[100], k=1;
results[0] = -1; //first member is -1
a.push(arr[j]); //push last array element to stack
for (int i=j-1; i>-1; i--)
{
while (a.top()<=arr[i]) //while top is not greater, pop elements
{
a.pop();
if (a.empty()) break;
}
if (a.empty()) //if it becomes empty, add -1 to results
{
results[k] = -1;
}
else //if larger element is found, add it to result
{
results[k] = a.top();
}
a.push(arr[i]); //push current element to stack
k++;
}
for (int i=k-1; i>-1; i--) cout<<results[i]<<" ";
cout<<endl;
}
int main()
{
int n;
cin>>n;
for (int i=0; i<n; i++)
{
int arr[100], j=0;
int input;
while (cin>>input) //add inputs to array until -1
{
if (input == -1) break;
else
{
(arr[j] = input);
j++;
}
}
j--;
NGE(arr, j);
}
}