#C7745. Minimum Absolute Difference Between Two Subarray Sums
Minimum Absolute Difference Between Two Subarray Sums
Minimum Absolute Difference Between Two Subarray Sums
You are given an integer N and an array A of N integers. Your task is to choose two non-overlapping contiguous subarrays such that:
- The first subarray is a non-empty prefix that starts at the first element and ends at index i (1 ≤ i < N).
- The second subarray is a non-empty suffix that starts at index j and ends at the last element (i+1 ≤ j ≤ N).
Let \( S_1 \) be the sum of the elements in the first subarray and \( S_2 \) be the sum of the elements in the second subarray. For each valid pair \((i, j)\), calculate the two quantities:
[ |S_1 - S_2| \quad \text{and} \quad |S_1 + S_2| ]
Your goal is to determine the minimum value among all these computed quantities. If N < 2, output -1
.
inputFormat
The first line contains an integer N indicating the number of elements in the array.
The second line contains N space-separated integers representing the array A.
outputFormat
Output a single integer representing the minimum value obtained from the expressions \(|S_1 - S_2|\) and \(|S_1 + S_2|\), taken over all valid pairs of subarrays. If there is no valid pair (i.e. when N < 2), output -1
.
6
2 -1 3 5 -3 2
0
</p>