Using monotonically increasing stack to solve..iterating from the back of array.....running fine for custom test cases but fails for all other

#include
#include<bits/stdc++.h>
using namespace std;
int sunny(vector v)
{
int n=v.size();
stack s;
for(int i=n-1;i>=0;i–)
{ if(s.empty())
{s.push(v[i]);}

else if(!s.empty()&&v[i]<s.top())
{s.push(v[i]);}

else
{
	while((!s.empty()&&v[i]>=s.top()))
   {	// cout<<s.top();
	   s.pop();
   
   }
	s.push(v[i]);
	
}
//cout<<endl;
}
return s.size();

}
int main() {
int t;
cin>>t;
vector x;
while(t–)
{
int n;
cin>>n;
vector v;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
v.push_back(x);
}
cout<<sunny(v)<<endl;
}
return 0;
}

Hey @abhigyanprakash29 share your code using ide.codingblocks.com so that i can help you in debugging it.

how should i send you the code

Go to ide.codingblocks.com , paste the code you want me to debug. Press save and share the new generated url here. I will debug it.

Check for this input:
1
17
87 78 16 94 87 94 50 63 28 91 64 27 93 27 73 12 69
Expected output is:
3
Yours is giving:
2
By this you will find your mistake

two building having same height beside each other …will both of them receive sunlight

Yes both of them will recieve sunlight

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like