You are not logged in. Please login at www.codechef.com to post your questions!

×

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

asked 03 Apr '18, 10:37

montycs's gravatar image

0★montycs
1057
accept rate: 0%

edited 06 Apr '18, 11:52


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

link

answered 03 Apr '18, 22:50

debjitdbb's gravatar image

4★debjitdbb
534
accept rate: 0%

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

(05 Apr '18, 11:22) montycs0★

Your code will fail for

8 6 6

9 9 198 9

try solving for these

link

answered 03 Apr '18, 12:33

amanrajok's gravatar image

3★amanrajok
111
accept rate: 0%

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

link

answered 03 Apr '18, 12:36

amanrajok's gravatar image

3★amanrajok
111
accept rate: 0%

please format your code..

(03 Apr '18, 22:25) montycs0★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×310

question asked: 03 Apr '18, 10:37

question was seen: 389 times

last updated: 06 Apr '18, 11:52