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

×

Need help in Hackerrank: Impressing the Boss

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 07 Apr '18, 15:35

montycs's gravatar image

0★montycs
1057
accept rate: 0%

1

4

13 15 12 14

(07 Apr '18, 16:55) bansal12325★

check out this test case:- 1 6 1 66 77 44 55 999. For this test case try all possible locations for which we will modify 1 to n, and try all possible values we want to use(min(a[i]) to max(a[i])) and finally we need to check the array is sorted or not.Time complexity will be O(pow(n,2)*max(a[i]).

link

answered 07 Apr '18, 18:13

sunilnitdgp5's gravatar image

1★sunilnitdgp5
1
accept rate: 0%

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: 07 Apr '18, 15:35

question was seen: 270 times

last updated: 07 Apr '18, 18:13