Two test cases coming wrong. I tried everything but still couldn.t get it

Here is my code, please check it.

#include
using namespace std;

int main () {
int N;
cin>>N;

int num;
int max , min;
int k = 1 , l = 1;

int counter;

for (counter = 1 ; counter <= N ; counter++) {
    cin>>num;
    if (counter == 1) {
        max = num;
        min = num;
    }

    else {
        if (max < num) {
            max = num;
            k++;
        }

        else if (min > num) {
            min = num;
            l++;
        }

        else {
            break;
        }
    }
}

if ((k == N || l == N) && (counter == N + 1)) {
    cout<<"true"<<endl;
}

else {
    cout<<"false"<<endl;
}

return 0;

}

Hello @sinchan1509_1a917d7b6cc3cbb9,

Your implementation is wrong,
For this test case it won’t work:

6
3 2 1 2 3 4

Here, the answer is true, because 321 forms a decreasing sequence and 1234 forms an increasing sequence.

ok i will implement this in my code

please don’t close the doubt section as if I will have any doubt I can ask .

I am not able to fix this test case, I tried a lot. Please help me. Here is my altered code:

#include using namespace std; int main () { int N; cin>>N; int num; int max , min; int k = 1; int l = 1; for (int counter = 1 ; counter <= N ; counter++) { cin>>num; if (counter == 1) { max = num; min = num; } else { if (max < num) { max = num; k++; continue; } else { if (min > num) { min = num; l++; continue; } else { break; } } } } if (k == N) { cout<<“true”<<endl; } else if (l == N) { cout<<“true”<<endl; } else if ((k + l) == N) { cout<<“true”<<endl; } else { cout<<“false”<<endl; } return 0; }

@sinchan1509_1a917d7b6cc3cbb9,

You can just iterate from i = 0 to n, till the sequence is strictly decreasing that is a[i-1]>a[i], and let’s say at index j this condition is violated.
Now iterate again from i = j to n, till the sequence is strictly increasing that is a[i-1]<a[i], and let’s say at index k this condition is violated.

Now, if the second condition is never violated that means we have successfully split the sequence into two, but if it is violated it means we can’t split the given sequence.

If you want I can send you my approach.

please send it but it is given that I can’t use arrays here

Hello @sinchan1509_1a917d7b6cc3cbb9,

You can find my implementation here ( I am not using arrays),

ok thank you so much

1 Like

I was able to solve my problem thank you so much

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.