#C8279. Maximum Contiguous Subarray Sum with Indices
Maximum Contiguous Subarray Sum with Indices
Maximum Contiguous Subarray Sum with Indices
You are given an array of n integers. Your task is to identify the contiguous subarray which has the maximum possible sum. In addition to finding the maximum sum, you must also output the starting and ending indices of this subarray.
If there are multiple subarrays with the same maximum sum, choose the one with the smallest starting index.
Mathematically, for an array \(a_0, a_1, \dots, a_{n-1}\), you need to find indices \(i\) and \(j\) such that the sum:
[ S = \sum_{k=i}^{j} a_k ]
is maximized.
inputFormat
The input is read from standard input (stdin) and has the following format:
n a0 a1 ... an-1
Where n
is a positive integer representing the number of elements in the array, and the next line contains n
space-separated integers.
outputFormat
Output the maximum subarray sum followed by the starting and ending indices of the subarray. The output should be printed to standard output (stdout) in a single line with each value separated by a space.
max_sum start_index end_index## sample
9
-2 1 -3 4 -1 2 1 -5 4
6 3 6
</p>