#C2863. Contiguous Subarray with Given Sum
Contiguous Subarray with Given Sum
Contiguous Subarray with Given Sum
You are given an array of integers and an integer ( k ). Your task is to determine if there exists a contiguous subarray whose elements sum exactly to ( k ). In other words, check whether there exist indices ( i ) and ( j ) (with ( 0 \le i \le j < n )) such that [ \sum_{l=i}^{j} a_l = k ]
If such a subarray exists, output YES
; otherwise, output NO
.
This problem tests your ability to use prefix sums and hash maps (or equivalent data structures) to achieve an efficient solution.
inputFormat
The first line contains two space-separated 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 elements.
outputFormat
Output a single line containing YES
if there exists a contiguous subarray with sum equal to ( k ), and NO
otherwise.## sample
5 10
1 2 3 4 5
YES