Dsa
Prefix Sum
Medium
Disjoint Mirror Sums
Given an array of N integers, count the number of pairs of indices (i, j) such that 0 <= i < j < N, where the sum of the prefix ending at i equals the sum of the suffix starting at j. The prefix and suffix must be disjoint.
Input Format:
The first line contains N.
The second line contains N integers.
Output Format:
A single integer representing the number of such pairs.
Example:
Input:
5
1 2 1 1 2
Output:
2
Explanation: Pair 1: Prefix [1, 2] sum=3, Suffix [1, 2] sum=3. Pair 2: Prefix [1, 2, 1] sum=4, Suffix [4] (index 2 to 2? No, disjoint means i < j). Let's check: i=1, j=3: sum(0..1)=3, sum(3..4)=3. i=2, j=4: sum(0..2)=4, sum(4..4)=2 (no). Only i=1, j=3 and i=0, j=4 (sum 1, suffix 2 - no).
Key concepts
prefix_sumhash_maplogic
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