forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HackerEarth_Signal_Range.cpp
67 lines (50 loc) · 970 Bytes
/
HackerEarth_Signal_Range.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
/*
Problem link :- https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/signal-range/
*/
#include <bits/stdc++.h>
using namespace std;
int search(int key,int a[],int i)
{
for(int j=i-1;j>=0;j--)
{
if(a[j]==key)
return j;
}
}
void previous_greater_equal(int a[],int n)
{
stack <int> s;
int ans[n],i;
ans[0]=1;
s.push(a[0]);
for(i=1;i<n;i++)
{
while(s.empty()==false && s.top()<=a[i])
s.pop();
if(s.empty())
ans[i]=i+1;
else
ans[i]=i-search(s.top(),a,i);
s.push(a[i]);
}
for(i=0;i<n;i++)
{
cout<<ans[i]<<" ";
}
}
int main() {
int n,i,j,k,t;
cin >> t;
while(t--)
{
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
previous_greater_equal(a,n);
cout<<endl;
}
return 0;
}