What is the error in my code?


Unable to clear any testcase

hi @goyalvivek
This problem is a variation of standard Longest Increasing Subsequence (LIS) problem. Let the input array be arr[] of length n. We need to construct two arrays lis[] and lds[] using Dynamic Programming solution of LIS problem. lis[i] stores the length of the Longest Increasing subsequence ending with arr[i]. lds[i] stores the length of the longest Decreasing subsequence starting from arr[i]. Finally, we need to return the max value of lis[i] + lds[i] – 1 where i is from 0 to n-1.

refer this code -->

yes, I have used the same approach, my sample imput provides correct ans as well, please check

hi @goyalvivek

l[i]++;
r[i]++;

remove these 2 lines, and make this change -

r[i] = max(r[i], r[j] + 1);
l[i] = max(l[i], l[j] + 1);

corrected code -->


It’s passing all test cases now…

hi @goyalvivek
I hope it’s clear now??

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.