← All problemsSign in

Valley Avoidance

CodeChefRating 2451Open on judge ↗

You are given a parameter $K$. A permutation $P$ of $\{1, 2, \ldots, N\}$ has a *valley* if there exist indices $i_1 \lt i_2 \lt\ldots\lt i_K$ such that $$ \max(P_{i_2}, P_{i_3}, \ldots, P_{i_{K-1}}) < \min(P_{i_1}, P_{i_K}) $$ That is, $P$ has a *valley* if there exists a sequence of $K$ indices such that the leftmost and rightmost elements are both larger than all the "middle" elements. ---

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