Help in understanding the problem

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;
}

Your code will fail for

8 6 6

9 9 198 9

try solving for these

TAKE TWO CASES FOR new value addding either to previous or to next
while(T–) {
int n;
cin>>n;
int arr[n],b[n];

for(int i=0;i<n;i++){
cin>>arr[i];
b[i]=arr[i];
}
int flag=0;
for(int i=1;i<n;i++){
if(arr[i-1]>arr[i]){

/if(arr[i-1]>arr[i+1]){
flag=1; wrong checking here consider the case: 8 6 6
break;}
/

arr[i]=arr[i-1],b[i-1]=b[i];
break;
//cnt++;
}
}
bool a_is_ok=true,b_is_ok=true;
for(int i=1;i<n;i++){
if(arr[i]<arr[i-1]){
a_is_ok=false;
break;
}
}
for(int i=1;i<n;i++){
if(b[i]<b[i-1]){
b_is_ok=false;
break;
}
}

if(a_is_ok || b_is_ok)
    cout<<"YES"<<"\n";
else cout<<"NO"<<"\n";

}
return 0;
}

I have followed a different approach and this is the link to my code. Hope that helps :slight_smile:

1 Like

please format your code…

I don’t get the bit where you have used del() function… I am not getting why you are comparing indexes instead of values…