Dsa
Prefix Sum
Medium
The Parity-Divisor Window
You are analyzing a sequence of financial transactions (positive for income, negative for expenses). Count how many subarrays have a sum that is divisible by a given integer K, and also contain an even number of negative transactions.
Input Format:
The first line contains N and K.
The second line contains N integers.
Output Format:
A single integer representing the total count of such subarrays.
Example:
Input:
4 3
3 -3 3 -3
Output:
4
Explanation: The subarrays are [3], [-3, 3], [3, -3], and [3, -3, 3, -3]. All have sums divisible by 3 and 0 or 2 negative numbers.
Key concepts
prefix_summathhash_map
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