Dsa
Prefix Sum
Medium
Synchronized Hiking
Two hikers, Alice and Bob, are walking parallel paths. We record their elevation gain/loss at every meter for N meters. Alice's changes are in array A, and Bob's are in array B. Count the number of intervals [i, j] where the total elevation change for Alice is exactly twice the total elevation change for Bob.
Input Format:
The first line contains N.
The second line contains N integers for Alice's path A.
The third line contains N integers for Bob's path B.
Output Format:
A single integer representing the count of intervals.
Example:
Input:
3
2 4 2
1 2 1
Output:
6
Explanation: Every subarray [i, j] satisfies sum(A[i..j]) = 2 * sum(B[i..j]). There are 6 such subarrays.
Key concepts
prefix_sumhash_maparrays
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now