please explain logic for this problem
Please explain logic for this problem
- Create inc[n] and dec[n]
- Initialize inc[0] to 1 and dec[n-1] to 1
a. Till end of the array ie, i=1 to n, if arr[i] > arr[i-1] then inc[i] = inc[i-1] + 1. else, inc[i] = 1
b. From the end of the array ie, i = n-2 till i =0, if arr[i] > arr[i+1] then dec[i] = dec[i +1] +1 else, dec[i] = 1
then final ans is
for (int i = 1; i < n; i++)
if (inc[i] + dec[i] - 1 > max)
max = inc[i] + dec[i] - 1;
can you please explain the logic behind this??
inc[i] means the maximum length increasing subarray till index i
dec[i] means the maximum length decreasing subarray till index i
so you take inc[i] + dec[i] - 1. means first increasing then decreasing.
sorry, but still not clear
can you please elaborate?
Can you solve this problem in O(N*N) ? if yes just try to visualise that.
if no, then for each i you will go the left till array is decreasing, and go to the right till array is decreasing.
we just preprocessed these values for each i.