#K78177. Subsequence Sum Query
Subsequence Sum Query
Subsequence Sum Query
You are given a list of positive integers S
and a target sum X. Your task is to determine whether there exists a continuous subsequence of S
whose sum is exactly X. In other words, you need to check if there exist indices l and r such that
\(\sum_{i=l}^{r} S[i] = X\)
If such a subsequence exists, output Yes
; otherwise, output No
. This problem can be efficiently solved using the sliding window (two pointers) technique.
inputFormat
The input is given from stdin in the following format:
- The first line contains two integers n and X, where n is the number of elements in the sequence and X is the target sum.
- The second line contains n positive integers representing the sequence
S
, separated by spaces.
outputFormat
Output to stdout a single line containing Yes
if there exists a continuous subsequence of S
whose sum is exactly X, or No
otherwise.
5 9
1 2 3 4 5
Yes