#K37312. Find Contiguous Subarray with Given Sum
Find Contiguous Subarray with Given Sum
Find Contiguous Subarray with Given Sum
Given an integer array \( nums \) and an integer \( target \), find a contiguous subarray whose sum is exactly \( target \). The indices of the subarray should be 1-indexed. If there exists such a subarray, output its starting and ending indices; otherwise, output -1
.
For example, if \( nums = [1, 2, 3, 4, 5] \) and \( target = 9 \), one valid contiguous subarray is \( [2, 3, 4] \) because \(2+3+4=9\), so the output will be 2 4
.
The solution should read input from standard input (stdin) and write the result to standard output (stdout).
inputFormat
The first line contains an integer \( n \) representing the number of elements in the array. The second line contains \( n \) space-separated integers denoting the elements of the array \( nums \). The third line contains an integer \( target \).
outputFormat
If a contiguous subarray exists that sums to \( target \), output two space-separated integers representing the starting and ending indices (1-indexed) of the subarray. Otherwise, output -1
.
5
1 2 3 4 5
9
2 4