please check:
it passes two cases but fails two
2 test cases not passing
you cannot solve this question by linearly iterating the array, as when same number appears multiple times in success they can satisfy the constraint both ways. you need to maintain additional increasing and decreasing state
Correct Approach:
You can make 2 array, one inc, that will have at ith position length of the increasing sequence till i, similary a dec array that will have length of decreasing sequence till ith (ith till n).
For inc array compute the sequence length from left to right
For dec array compute the sequence length from right to left.
Now for every i you have both increasing length till that point and decreasing length to get answer.
Eg
1 6 8 9 3 4 6 5
inc array
1 2 3 4 1 2 3 1
dec array
1 1 1 2 1 1 1 2
Ans 4 + 2 -1
sir i have considered equal elements pls read the code
instead of if(n == 2) else if (n==2) would come