# PROBLEM LINK:

Contest Division 1

Contest Division 2

Contest Division 3

Contest Division 4

Setter: Kartik Malik

Tester: Abhinav Sharma, Aryan

Editorialist: Lavish Gupta

# DIFFICULTY:

Easy

# PREREQUISITES:

Longest Increasing Subsequence

# PROBLEM:

Chef received an array A of N integers as a valentine’s day present. He wants to **maximize** the length of the **longest strictly increasing subsequence** by choosing a **subarray** and adding a fixed integer X to all its elements.

More formally, Chef wants to **maximize** the length of the longest strictly increasing subsequence of A by performing the following operation at most once:

- Pick 3 integers L, R and X \ (1 \leq L \leq R \leq N, \ X \in \mathbb{Z}) and assign A_i := A_i + X \ \forall \ i \in [L, R].

Find the length of the longest **strictly** increasing sequence Chef can obtain after performing the operation **at most once**.

# EXPLANATION:

Let say that the Chef has chosen L, R and X, and have assigned A_i := A_i + X \ \forall \ i \in [L, R] to get the optimal answer. Also, assume that X \gt 0 for the current discussion. We will handle the other case later. So we can divide our array into three parts: A[1 \ldots L-1], A[L \ldots R], A[R+1 \ldots N]

Now, let say B = \{A_{i_1} \ldots A_{i_x}, A_{j_1} \ldots A_{j_y}, A_{k_1} \ldots A_{k_z} \} be the final LIS, such that i_u \in [1, L-1], j_u \in [L, R], k_u \in [R+1, N]. Observe that if we further increase all A_i for R \lt i \leq N, the length of resulting LIS will either remain same as that of B, or it will increase. In simple words, increasing all A_i for R \lt i \leq N only improves our solution.

So we can claim that if X \geq 0, we will only increase a suffix in the optimal case. If X \lt 0, we can have a similar analysis to show that we will only decrease the prefix, which is equivalent to increasing the remaining suffix by same amount, as long as LIS is considered. So, we only need to analyze X \geq 0, and R = N.

Let us now try to analyze what happens when we fix L. What is the maximum length of LIS that we can get. Now, we can divide our array into two parts: A[1 \ldots L-1], A[L \ldots N].

Let B_1 = \{A_{i_1} \ldots A_{i_x} \} and B_2 = \{ A_{j_1} \ldots A_{j_y} \} be the LIS of A[1 \ldots L-1] and A[L \ldots N] respectively. If we choose X in such a way that A_{i_x} \lt A_{j_1} + X, we can have our final LIS as B_1 + B_2, where the + sign denotes the concatenation. Observe that this is the maximum length of LIS that we can get for this specific L.

Hence, for each i such that 1 \leq i \leq N, we can first calculate dp_1[i] = Length of LIS of A[1 \ldots i] and dp_2[i] = Length of LIS of A[i \ldots N], and take the maximum of dp_1[i] + dp_2[i+1] for all valid i.

# TIME COMPLEXITY:

To calculate length of LIS for each i, we will require O(N \cdot \log{N}) time. To further calculate answer, we will require O(N) time. Hence the overall time complexity is O(N \cdot \log{N}) for each test case.

# SOLUTION:

Editorialist’s Solution

Setter’s Solution

Tester-1’s Solution

Tester-2’s Solution