← All problemsSign in

Balanced Subarrays

CodeChefRating 2495Open on judge ↗

An array $A$ of length $3N$ is said to be *balanced* if it contains exactly $N$ occurrences each of $1,2,$ and $3$. For example, the arrays $[1, 2, 1, 3, 3, 2]$ and $[3, 2, 1]$ are *balanced*, whereas $[1, 1, 1], [1, 2, 1, 2, 1, 2],$ and $[3, 2, 1, 1, 3, 3]$ are not. You are given an integer $K$ ($1 \leq K \leq 10^7$). Construct *any* array $A$ of length **at most** $10^4$, consisting of only

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.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start