Is there any more elegant way to solve this problem?

My solution to this problem-

#include
using namespace std;
int main() {
int n,num;
cin>>n>>num;
int temp=num;
int j=0,k=0;

for(int i=2;i<=n;i++)
{
	cin>>num;
	if(temp>num and i==j+2)
		j++;
	else if( temp<num and i==j+k+2)
		k++;
	else if(temp==num and i==j+2)
		k++;
}
if(j+k==n-1)
	cout<<"true"<<endl;
else
	cout<<"false"<<endl;
return 0;

}

Hello @ak7170047,

I don’t know if the following approach is elegant or not.:wink:
But, it is surely a different approach to the problem:

Give a like if you are satisfied.

1 Like