Increasing Decreasing Check

Can you please tell me what’s wrong with my code?

import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int first = scn.nextInt(); boolean isValid = true; boolean isDecreasing = true;
    while(n!=1) {
        int second = scn.nextInt();
        if (first == second) {
            isValid = false;
            break;
        } else if (first > second) {
            isDecreasing = false;
        } else if(!isDecreasing && first < second) {
            isValid = false;
            break;
        }
        first = second;
        n--;
    }
    System.out.println(isValid);
}

}

Here is the link to my code https://ide.codingblocks.com/s/608556

Hey @srbh453,
You used the opposite signs in your logic.

This condition is incorrect as till this point the series is already decreasing and you are checking if the series is decreasing(if first> second then then series is decreasing) which is incorrect.

Similarly, with the other case to, you need flip the sign as if the series is already increasing and you are checking if the series is increasing(if first< second then then series is increasing) which is incorrect.

Here’s your updated code. I have added a few comments for your reference.


Hope it helps.

I understand. 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.