Increasing Decreasing Sequence

5 test cases passed 4 failed

The question says that an input sequence will be given to you and you have to return true if the sequence follows a pattern such that it first increases(like 1,2,3,4) and then decrease(3,0). So if the input is 1,2,3,4,3,0 then return true else false.

You can do it by siimply iterating till the next number is greater than the present number and then check the same for the decreasing sequence.

1 Like

@S18ML0016 see my code

Do not break the code in first loop when x{i+1} > x[i] because it is expected rather break when x[i+1] <x[i] and update num outside of the if condition.
Secondly in the second loop start i with num-1 rather than num.


5
1 2 3 4 5
excpected;true
compiler showing:false

code is still not working

No, In the first loop only put update= i+1 outside the if condition and break inside the if condition. also change the if condition with x[i+1] < x[i] and the same thing with the next loop.

@Ankur-Ankur-937479863269311
Think of it this way. There are three possibilities in total

  1. The sequence is completely increasing.
  2. The sequence is completely decreasing
  3. The sequence is first decreasing then increasing.

These are the three situations where we print true or else we print false.
If we plot the graph for the three situations the first one will be a simple increasing curve.
The second one will be a simple decreasing curve.
Both of these cases can be checked and verified using a simple loop.

The only case you are left to check now is the third one which forms a V-like graph , first decreasing then increasing.
To check this we can see how many turning points are there in the sequence. A turning point would be the element before which all elements were increasing and from thereon start decreasing or before which all elements were decreasing and then start increasing.
Assume your sequence to be decreasing at first since that is specified in the problem that the sequence is first decreasing. After that check for such turning points. From the graph we can infer that this would be the lowermost point in the V-graph. Also there would be only one such point atmost and no more. So simply count such turning points. Check if the no of turning points is more than 1. If so , print false. Else print true.