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.