#C10613. Contiguous Subarray Sum Query
Contiguous Subarray Sum Query
Contiguous Subarray Sum Query
You are given an array A of n integers and q queries. For each query with an integer value k, determine whether there exists a contiguous subarray of A whose sum is exactly k.
A contiguous subarray is defined as a subarray that consists of consecutive elements from the array. In other words, if the subarray starts at index i and ends at index j (with 0-based indexing), its sum is given by the formula
\( S = \sum_{t=i}^{j} A[t] \)
Your task is to output YES
if such a subarray exists for the given query, and NO
otherwise.
Note: The input will be provided via standard input (stdin
) and you must output your results to standard output (stdout
), one result per query on a new line.
inputFormat
The first line of input contains two space-separated integers, n and q, representing the number of elements in the array and the number of queries, respectively.
The second line contains n space-separated integers representing the array A.
The third line contains q space-separated integers, where each integer corresponds to a query value k.
outputFormat
For each query, output a single line containing YES
if there exists a contiguous subarray whose sum is exactly k, and NO
otherwise.
5 3
1 2 3 4 5
9 15 20
YES
YES
NO
</p>