#C1258. Find Contiguous Subarray with Given Sum
Find Contiguous Subarray with Given Sum
Find Contiguous Subarray with Given Sum
Given an array of positive integers and a target sum \(T\), find the first contiguous subarray whose sum equals \(T\). If such a subarray exists, output its elements (in the order they appear in the array) separated by spaces; otherwise, output -1.
The problem can be solved efficiently using a two-pointer (sliding window) technique. The array contains only positive integers, which guarantees that when the current window sum exceeds \(T\), it can safely be reduced from the left.
inputFormat
The first line contains an integer \(n\) representing the length of the array. The second line contains \(n\) space-separated positive integers representing the array elements. The third line contains the target sum \(T\).
Example:
5 1 2 3 7 5 12
outputFormat
If a contiguous subarray with sum equal to \(T\) is found, output the elements of the subarray in a single line separated by spaces. If no such subarray exists, output -1.
Example:
2 3 7## sample
5
1 2 3 7 5
12
2 3 7