Distinct 2 Subarray
An array is called *good* if it has exactly $2$ distinct elements. For example, $[1, 1, 2, 1, 2], [5, 3]$ are good but $[1, 2, 3]$ and $[1, 1, 1]$ are not. You are given an array $A$ of $N$ elements and you should find the length of the **shortest** possible good subarray$^{\dagger}$ of $A$. If no good subarray exists, print $-1$ instead. --- $^{\dagger}$ $B$ is said to be an subarray of $A$ i
HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17
CodeSearch Tutor
Hints, not spoilers — it won’t hand over the full solution unless you insist.
Sign in to chat with the tutor and save your progress.
Sign in to start