Count Subarrays with Target Sum Difference
Given an array of N integers, find the number of subarrays whose sum has a specific difference K from the sum of the remaining elements. That is, for a subarray A[i...j], we want to count how many pairs (i, j) exist such that sum(A[i...j]) - sum(A[0...i-1]) - sum(A[j+1...N-1]) = K.
Input
The first line contains two integers N and K. The second line contains N space-separated integers representing the array elements.
Output
A single integer representing the count of such subarrays.
Constraints
1 <= N <= 200000, -10^9 <= A[i] <= 10^9, -10^15 <= K <= 10^15.
Example 1
5 2 1 2 3 4 5
0
Total sum S=15. Target sum = (15+2)/2 is not an integer. No solution, answer 0. Oh wait, S+K = 17, odd. The code logic needs to be correct. Let's re-evaluate. (S+K)/2 = (15+2)/2.5. Ah, (S+K) must be even. So if S+K is odd, answer is 0. Correct calculation: S=15, K=2. S+K=17 (odd). Answer is 0. Sampl
Example 2
5 1 1 2 3 4 5
0
Total sum S=15. Target sum = (15+1)/2 = 8. Subarrays with sum 8: [3, 4, 1] no, [3,4] no, [3,5] no. Subarrays: [1,2,3,4,5] sum=15. [1,2,3,4] sum=10. [2,3,4,5] sum=14. [1,2,3] sum=6. [2,3,4] sum=9. [3,4,5] sum=12. [1,2] sum=3. [2,3] sum=5. [3,4] sum=7. [4,5] sum=9. [1] sum=1. [2] sum=2. [3] sum=3. [4]
Example 3
3 6 1 2 3
1
Total sum S=6. K=6. Target sum = (6+6)/2 = 6. The subarray [1, 2, 3] has sum 6. Remaining sum is 0. Difference is 6 - 0 = 6. Count is 1.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 2 1 2 3 4 5
5 1 1 2 3 4 5
3 6 1 2 3