My code failed for 3 test cases


Please correct it.

Why is it not being solved?

@Utkarshsingh5298291,
Input:
5
5 4 3 2 1

Correct output:
true

Your output:
false

Here increasing sequence will be empty.

  • A sequence is true if you can split it into two sub sequence such that first sequence is strictly decreasing and second sequence is strictly increasing.

  • For e.g.,
    1 2 3 4 5
    This sequence is also true as we can split it into two sequence like., sequence one is empty and sequence two is 1 2 3 4 5.

  • Let’s take another example.,
    5 4 3 2 1
    This is also true as we can split it such that sequence one is5 4 3 2 1 and sequence two is empty.
    According to the problem statement, we can say the if the sequence decreases then it should not increase, if this is the case one can directly print false else print true.

  • The third case is when the sequence is first strictly decreasing then strictly increasing.
    For example :
    9 5 2 7 10
    The sequence first decreases starting from 9 to 2 - { 9, 5, 2 } and then starts increasing - { 7, 10}. Note that it can also be broken as { 9, 5} and { 2, 7, 10} .
    Since the sequence fulfills the required condition , we would also print “true” for this.

These are 3 cases you have to consider. Also in case all elements are equal the answer will be false which your code is giving correct.

thank you Sanchit Sir for help…You were bit late though.

Sombody pls ,this question is still not running for all the test cases…

@Utkarshsingh5298291,
What doubt are you having?

not running for one testcase

@Utkarshsingh5298291,
I think you’ve sent the wrong code. Please check and send again

I have sent the right code.

@Utkarshsingh5298291,

This is the code I am seeing. Please update it.

Bro yahi code hai ek baar chala k to dekho yaar …kya kar rhe ho aap?

89% test cases chal rhe hai…

  while(t-->0)
	{
		
	int n=sc.nextInt();
    
	if(sum<n)
	{
		b=true;
	}
	else{
		b=false;
	 
	}
	sum=n;
	
	}

This is your code … try to debug it carefully …
If you read the problem carefully, you have to identify if the given sequence first increase and decrease…
Thats it!
So, there are two corner cases you should check separately
if {Case1 if the sequence is only increasing / decreasing return true;
else if {Case2 if the sequence increasing and decreasing return true;
else if{return false;

Your code:
as soon as it see decrement it assigns b=false; and as soon as it see increment it assigns b=true;
So, verdict test case:
Input:
7
1 7 4 10 1 2 3
your output: true
But you, yourself think is it correct , the input sequence given above is increasing then decreasing again increasing , then again decreasing and lastly its increasing…

@Utkarshsingh5298291
Your algorithm is wrong. The testcases seem to be weak. I will update them.
Your code does not take into account the current state of the series i.e. whether it is increasing or decreasing. It simply compares two consecutive elements. Your final verdict is based on the comparison of just the last two elements of the sequence taking in no account of the sequence before them. Your code only checks whether last 2 elements are in increasing order and that’s it. All previous computations of variable b are overwritten and nothing else matters.
Consider this testcase
6
4
5
7
1
2
3

Expected output is false however your code gives true.

Kindly refer to my response on this thread for an explanation of this problem - I am not able to find the logic of this problem