#K9996. Equal Coin Split
Equal Coin Split
Equal Coin Split
You are given a collection of coins with positive integer values. The coins are arranged in a row. Your task is to determine whether it is possible to split the collection into two non-empty contiguous parts such that the sum of the coin values in the first part equals the sum of the coin values in the second part.
If a valid split exists, output YES x
where x
is the number of coins in the first part. Otherwise, output NO
.
Note that the split must divide the list into two consecutive segments. The input starts with an integer n representing the number of coins, followed by n positive integers that denote the coin values. The solution should work for both small and large arrays efficiently.
Mathematically, let \(a_1, a_2, \ldots, a_n\) be the coin values. You need to find an index \(i\) (with \(1 \le i < n\)) such that:
[ \sum_{j=1}^{i} a_j = \sum_{j=i+1}^{n} a_j ]
If such an index exists, output YES i
(using 1-indexing for the first part length); otherwise output NO
.
inputFormat
The first line of input contains an integer n (1 ≤ n ≤ 105), representing the number of coins. The second line contains n space-separated positive integers representing the values of the coins.
outputFormat
Output a single line. If there is a valid split, output YES x
where x
is the number of coins in the first part; otherwise, output NO
.
6
1 2 3 3 2 1
YES 3
</p>