please explain how to solve this question in O(n)
Please explain how to solve this question in O(n)
Hey @KetanPandey
Approach for O(n)
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
max(1+1-1,2+1-1,3+1-1,…)
Ans 4 + 2 -1