What is the problem in my code 3 test cases rest all are pass

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);

	int N = sc.nextInt();
	int[] a = new int[N];
	boolean status = false;

	for (int i = 0; i < a.length; i++) {
		if(N<0){
          status=false;
		  break;
		}
		a[i] = sc.nextInt();
	}

	if (a[0] < a[1] && a[a.length - 2] <= a[a.length - 1]) {
		status = true; // true if series is increasing
	} else if (a[0] >= a[1] && a[a.length - 2] > a[a.length - 1]) {
		status = false; // false if series is decreasing
	}
	else if(a[0]<=a[1] && a[a.length-2]>=a[a.length-1]) {
		status=false;   // false if series is increasing then decresing
	}
	else {
		status=false; // false if series is decresing then increasing
	}

	if (status) {
		System.out.println("true");
	} else {
		System.out.println("false");
	}
}

}

@nigamshubham1998,

  1. Test case:
    Input:
    5
    5 4 3 2 1
    Correct answer:
    true
    Your answer:
    false
  2. Test case:
    Input:
    5
    5 5 5 5 5
    Correct answer:
    false
    Your answer:
    true

Optimal Approach:

  • 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.

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.