Sum of Second Max
For an array $B$, we define $f(B)$ as the second largest element of the array. For example, $f([2, 4, 5, 1]) = 4$. You are given a permutation array $A$ of $N$ integers (Each element from $[1, N]$ occurs exactly once). Compute the sum of $f$ of all subarrays of $A$ of size at least $2$. Formally, compute the following sum: $$\sum_{L = 1}^{N} \sum_{R = L + 1}^{N} f([A_L, A_{L + 1}, \ldots, A_R]$
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