Help in understanding the problem

hackerrank

#1

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* < 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*;
        printAnswer(a,n);
    }
    return 0;
}

#2

Your code will fail for

8 6 6

9 9 198 9

try solving for these


#3

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*;
b*=arr*;
}
int flag=0;
for(int i=1;i<n;i++){
if(arr[i-1]>arr*){

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

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

if(a_is_ok || b_is_ok)
    cout<<"YES"<<"

“;
else cout<<“NO”<<”
";

}
return 0;
}


#4

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


#5

please format your code…


#6

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