# EQLZR - Editorial

**URL:** <https://discuss.codechef.com/t/eqlzr-editorial/126095>\
**Category:** editorial\
**Tags:** editorial, start262\
**Created:** [October 28, 2026, 5:30pm UTC](https://discuss.codechef.com/t/eqlzr-editorial/126095 "2026-10-28T17:30:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [October 28, 2026, 5:30pm UTC](https://discuss.codechef.com/t/eqlzr-editorial/126095/1 "2026-10-28T17:30:20Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/EQLZR)  
[Contest: Division 1](https://www.codechef.com/START262A/problems/EQLZR)  
[Contest: Division 2](https://www.codechef.com/START262B/problems/EQLZR)  
[Contest: Division 3](https://www.codechef.com/START262C/problems/EQLZR)  
[Contest: Division 4](https://www.codechef.com/START262D/problems/EQLZR)

_ **Author:** _ [nikhil\_010309](https://www.codechef.com/users/nikhil_010309)  
_ **Tester:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

TBD

# PREREQUISITES:

None

# PROBLEM:

You’re given an array A.  
At most once, you can choose any subarray and increment all its elements by 1.

Find the maximum possible number of equal adjacent pairs in the array.

# EXPLANATION:

Let’s call index i _good_ if A\_i = A\_{i+1}.  
Our objective is to maximize the number of good indices in the array.

Suppose we choose to increment the elements of the subarray A[L, R].  
Observe that:

- If L \gt 1 and index L-1 was good earlier, it will now become not good; since A\_L changed.  
Similarly, if R \lt N and R was good, it will become not good.
- On the other hand, if L \gt 1 and L-1 was _not_ good earlier, it’s possible for L-1 to become good; if A\_L becomes equal to A\_{L-1} after the increment.  
Similarly, if R wasn’t good earlier, it might become good now if it equals A\_{R+1}.
- No other index can change its good-ness!
  - For L \le i \lt R, both A\_i and A\_{i+1} will increase by 1, so they’ll be equal afterwards iff they were equal to start with.
  - For i \lt L-1 and i \gt R, A\_i and A\_{i+1} don’t change at all.

Basically, only the good-ness of the indices L-1 and R can change at all; and the change at L-1 does not depend on the choice of R (and vice versa).

* * *

This gives us a fairly simple solution: iterate over R from left to right, while also maintaining the maximum “profit” so far from choosing some L \le R.  
Then update the answer with the profit at R plus the maximum profit from L \le R.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n = int(input())
> a = list(map(int, input().split()))
>     
> base, add, mx = 0, 0, 0
> for i in range(n):
> if i+1 < n and a[i] == a[i+1]: base += 1
>         
> if i > 0:
> if a[i-1] == a[i]+1: mx = 1
>         
> if i+1 < n:
> if a[i]+1 == a[i+1]: add = max(add, 1+mx)
> elif a[i] == a[i+1]: add = max(add, -1+mx)
> else: add = max(add, mx)
> else: add = max(add, mx)
> print(base + add)
> 
> ```
