I am trying to solve this problem: Impressing the Boss. I am simply checking if the next element is smaller than the current element and this approach seems to pass half of the test cases but it is failing some. Please help in understanding the error in my approach. Here is my implementation:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void printAnswer(int a[], int n)
{
int c = 0;
for(int i=1;i<n;i++)
{
if(a[i] < a[i-1])
c++;
}
if(c > 1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
int main()
{
int t;
cin>>t;
while(t --)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
printAnswer(a,n);
}
return 0;
}