#K33087. Count Maximum Length Contiguous Subsequences
Count Maximum Length Contiguous Subsequences
Count Maximum Length Contiguous Subsequences
Given an array of integers \(arr\) and an integer \(K\), you are to find the number of contiguous subsequences (i.e., subarrays) whose sum is equal to \(K\) and which have the maximum possible length among all such subsequences.
More formally, consider all pairs \((i,j)\) (with \(0 \le i \le j < n\)) such that \(\sum_{t=i}^{j} arr[t] = K\). Let \(L = j-i+1\) be the length of a subsequence. Your task is to determine the number of subsequences for which \(L\) is maximum. If there exists no subsequence with sum equal to \(K\), output 0.
inputFormat
The input consists of two lines:
- The first line contains two integers \(N\) and \(K\), where \(N\) is the number of elements in the array and \(K\) is the target sum.
- The second line contains \(N\) space-separated integers representing the array \(arr\).
outputFormat
Output a single integer on a line by itself representing the number of contiguous subsequences with sum equal to \(K\) that have the maximum possible length among all such subsequences. If no such subsequence exists, output 0.
## sample5 5
1 2 3 2 1
2