#K33882. Closest Subarray Sum
Closest Subarray Sum
Closest Subarray Sum
You are given a list of weights arranged in a line and a target value. Your task is to find a contiguous subarray such that the sum of its elements is as close as possible to the target value. The answer should be reported as two indices (1-based) indicating the start and end of the subarray.
If there are multiple subarrays with the same difference from the target, choose the one with the smallest starting index. If there is still a tie, choose the one with the smallest ending index.
Note: A subarray is a contiguous segment of the array.
Mathematical Formulation:
Given an integer \( n \) and an integer \( T \) (the target), along with an array \( a_1, a_2, \dots, a_n \), find indices \( i \) and \( j \) (with \( 1 \leq i \leq j \leq n \)) such that \[ |\sum_{k=i}^{j}a_k - T| \quad \text{is minimized.} \]
inputFormat
The input is given via standard input (stdin) and consists of two lines. The first line contains two integers \( n \) (the number of elements) and \( T \) (the target sum), separated by a space. The second line contains \( n \) integers representing the weights, separated by spaces.
outputFormat
The output should be printed to standard output (stdout) as two integers separated by a space. These integers represent the 1-based starting and ending indices of the chosen subarray.
## sample5 7
3 1 4 2 5
2 4
</p>