# TELEPHN - Editorial

**URL:** <https://discuss.codechef.com/t/telephn-editorial/126136>\
**Category:** editorial\
**Tags:** easy, editorial, start263\
**Created:** [November 4, 2026, 4:30pm UTC](https://discuss.codechef.com/t/telephn-editorial/126136 "2026-11-04T16:30:17Z")\
**Posts on this page:** 4\
**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:** [November 4, 2026, 4:30pm UTC](https://discuss.codechef.com/t/telephn-editorial/126136/1 "2026-11-04T16:30:17Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/TELEPHN)  
[Contest: Division 1](https://www.codechef.com/START263A/problems/TELEPHN)  
[Contest: Division 2](https://www.codechef.com/START263B/problems/TELEPHN)  
[Contest: Division 3](https://www.codechef.com/START263C/problems/TELEPHN)  
[Contest: Division 4](https://www.codechef.com/START263D/problems/TELEPHN)

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

# DIFFICULTY:

TBD

# PREREQUISITES:

Binary search, sliding window

# PROBLEM:

You are given a circular array A.  
Every second, each element simultaneously takes on the larger of its neighbors’ values; forgetting its own.  
Find the first instant of time at which all elements become equal; or decide that this never happens.

# EXPLANATION:

Let M = \max(A) be the largest element in A.  
Observe that the value M will always be present in the array, since any neighbor of M will surely take up the value M.

So, if all elements are equal in the end, they must all have value M.

* * *

Let us now understand what exactly the value at some index will be, after several seconds have passed.

Define A\_i^{(t)} to be the value at index i after t seconds.  
Then we have:

- A\_i^{(0)} = A\_i
- A\_{i}^{(t)} = \max\left(A\_{i-1}^{(t-1)}, A\_{i+1}^{(t-1)}\right)  
Of course, array indices are considered cyclically here.

If you work this out for a few small values of t, it can be verified that the following holds:

> A\_i^{(t)} equals the maximum value among A\_{i-t}, A\_{i-t+2}, A\_{i-t+4}, \ldots, A\_{i+t-2}, A\_{i+t}

That is, after t seconds, the value at index i will equal the maximum value that can be reached by taking a walk of **exactly** t steps starting at i, with it being allowed to visit indices repeatedly.

This can be proved via the recurrence above for A\_i^{(t)}.

In particular, observe that A\_i^{(t)} \le A\_i^{(t+2)} since, the set of positions available to index i at t+2 includes the positions available to it at t.

* * *

This observation, along with the initial one of the final state being all elements equal to M, gives a fairly simple solution.

Let’s consider only even values of t.  
Because A\_i^{(t)} \le A\_i^{(t+2)}, once some element becomes M at an even t it will remain M for all larger even times too.  
This allows us to binary search for the smallest even t where everything becomes M.

Performing the check for the binary search requires us to compute \max(A\_{i-t}, A\_{i-t+2}, \ldots, A\_{i+t}) for each i, which is just a [sliding window maximum](https://usaco.guide/gold/sliding-window?lang=cpp) problem on the circle.

In fact, we don’t even need the full power of the sliding window maximum deque; we only need to know whether each window like this contains at least one occurrence of M or not so just keeping a counter for M is enough to solve the problem, rather than a monotonic deque and whatnot.  
This simplifies implementation a fair bit.

We can do the same for odd t too, and take the best answer among them.

It’s also possible to solve the problem in linear time, if you wish to try.

# TIME COMPLEXITY:

\mathcal{O}(N\log N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> def check(a, k): # k elements
> n, M = len(a), max(a)
>     
> for st in [0, 1]:
> ct, l, r = 0, st, st
> for i in range(k):
> ct += a[r] == M
> r = (r + 2) % n
> if ct == 0: return False
>         
> for i in range(n):
> ct -= a[l] == M
> ct += a[r] == M
> l = (l + 2) % n
> r = (r + 2) % n
> if ct == 0: return False
> return True
>     
>     
> 
> for _ in range(int(input())):
> n = int(input())
> a = list(map(int, input().split()))
>     
> if min(a) == max(a):
> print(0)
> continue
>     
> M = max(a)
> ans = 10**9
>     
> # even time
> lo, hi = 1, n+5
> while lo < hi:
> mid = (lo + hi)//2
>         
> # check length 2*mid -> 2*mid+1 elements
> if check(a, 2*mid+1): hi = mid
> else: lo = mid+1
> if check(a, 2*lo+1): ans = min(ans, 2*lo)
>     
> # odd time
> lo, hi = 1, n+5
> while lo < hi:
> mid = (lo + hi)//2
>         
> # check length 2*mid-1 -> 2*mid elements
> if check(a, 2*mid): hi = mid
> else: lo = mid+1
> if check(a, 2*lo): ans = min(ans, 2*lo-1)
>     
> if ans == 10**9: print(-1)
> else: print(ans)
> 
> ```

---

<div class="post-metadata">

**Author:** ![siddhu2502](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@siddhu2502](https://discuss.codechef.com/u/siddhu2502)\
**Post date:** [November 4, 2026, 4:39pm UTC](https://discuss.codechef.com/t/telephn-editorial/126136/2 "2026-11-04T16:39:47Z")

</div>

missing code in the editorial

---

<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:** [November 5, 2026, 5:06am UTC](https://discuss.codechef.com/t/telephn-editorial/126136/3 "2026-11-05T05:06:20Z")

</div>

Apologies for missing that, has been added now.

---

<div class="post-metadata">

**Author:** ![sathvik\_cc](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@sathvik\_cc](https://discuss.codechef.com/u/sathvik_cc)\
**Post date:** [November 5, 2026, 2:13pm UTC](https://discuss.codechef.com/t/telephn-editorial/126136/4 "2026-11-05T14:13:05Z")

</div>

Is there any other way to show that condition is valid without using small values ?
