Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Try using dynamic programming.
Let <code>dp[i]</code> be the number of alternating subarrays ending at index <code>i</code>.
The final answer is the sum of <code>dp[i]</code> over all indices <code>i</code> from <code>0</code> to <code>n - 1</code>.