A conveyor carries n parcels in a fixed order, each with an integer weight (a negative weight models a tare/credit adjustment). You must cut the belt at exactly one point so that every parcel before the cut goes to bin A and every parcel after the cut goes to bin B. Both bins must receive at least one parcel, so the cut is placed after position 1, 2, ..., or n-1. Report the minimum possible absolute difference between the total weight in bin A and the total weight in bin B.
Line 1: an integer n.
Line 2: n space-separated integers, the parcel weights in belt order.
A single integer: the minimum absolute difference between the two bins' total weights.
Example 1
Input
5 1 2 3 4 5
Expected
3
Explanation
Cutting after position 3 gives bin A = 1+2+3 = 6 and bin B = 4+5 = 9, a difference of 3, which is the smallest possible.
Example 2
Input
2 10 -10
Expected
20
Explanation
The only cut gives bin A = 10 and bin B = -10, an absolute difference of 20.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →