#K59627. Find Continuous Subarray with Target Sum
Find Continuous Subarray with Target Sum
Find Continuous Subarray with Target Sum
You are given an array of integers and an integer target. Your task is to find the first continuous subarray such that the sum of its elements equals the target value. In mathematical notation, you need to find indices \(l\) and \(r\) (with \(l \le r\)) such that \(\sum_{i=l}^{r} nums[i] = target\). If multiple subarrays satisfy the condition, choose the subarray which appears first. If there is no subarray that sums to the target, output -1.
Note: The input is read from stdin
and the output should be printed to stdout
.
inputFormat
The first line contains two integers: n (the number of elements in the array) and target. The second line contains n space-separated integers representing the array elements.
outputFormat
If a continuous subarray with sum equal to target exists, print its elements separated by a space on one line. If no such subarray exists, print -1.## sample
5 12
1 2 3 7 5
2 3 7