Increasing decrease sequence

Take the following as input.

A number (N)
Take N more numerical inputs
The N inputs for a sequence S = s1, s2, …, sN. Compute if it is possible to split sequence into two sequences -
s1 to si and si+1 to sN such that first sequence is strictly decreasing and second is strictly increasing. Print true/false as output.

public static boolean valid_invalid(int n){

    int prev = scn.nextInt();
    int cnt = 1;
    boolean goingUp = false;
    boolean isValid = true;

    while(cnt < n){

        int curr = scn.nextInt();

        int diff = curr - prev;
         // As the sequence must be strctly decreasing or increasing
        if(diff == 0){  
            isValid = false;
        }else if(diff > 0){  // If Sequence is increasing 
            goingUp = true;
        }else if(goingUp && diff < 0){
          // If Sequence is Decreasing if its already increased once
                            isValid = false;
        }

        prev = curr;
        cnt++;

    }

    return isValid;

}
sir i think the above solution to this problem is not in c++. can u give it in c++ language with explaination.

Hello @Sheenagoyal21,

In the above question, you have to consider following:

  1. strictly increasing eg: 5 1 2 3 4 5
  2. strictly decreasing eg: 5 5 4 3 2 1
  3. strictly decreasing then strictly increasing eg: 3 2 1 2 3
  4. strictly increasing then strictly decreasing eg: 1 2 3 2 1

for 1., 2., 3. case you should print true and
for case 4. and rest, it should print false.
Note: strictly words means equal numbers at adjacent positions would not be accepted. eg: 5 3 2 2 1 0

I am here to help you in understanding the logic.
Thus, i would suggest you to implement above mentioned cases and send your code if you face any issue.
I would help you in resolving the error if any occurs.

Here is the required code. But i would insist you to try it yourself first.
By looking at the answer without trying really hard, wont build any logic.

Hope, you are getting what i said.
Give a like, if you are satisfied.

4 Likes

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.

My code is passing all the test cases, just not the test case 0. I don’t understand the issue. Please help.

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner s = new Scanner(System.in);

	int n = s.nextInt(); // 5

	int previous = s.nextInt(); // 1
	boolean value = true;

	for(int i = 1; i <= n -1 ; i++)
	{
		int current = s.nextInt(); // 2,3,4,5 or 2,3,1,0
		
		if(current == previous) 
		{
			value = false;
		}
		else if(current > previous)
		{
			value = true;
		}
		else if(current < previous || current < 0)
		value = false;

		previous = current; // 2, 
	}

	if(value == true)
	{
		System.out.println(value);
	}
	else
	System.out.println(value);
}

}