You are auditing a ledger that records n transaction amounts in chronological order. During the audit you repeatedly settle the two transactions currently at the front of the (still-unsettled) ledger together, and the settlement total of that action is the sum of the two amounts involved.
The very first settlement you perform fixes the required total for the rest of the audit session: every settlement after that must produce exactly this same total. Starting from the front of the ledger, keep settling the next two still-unsettled entries for as long as each new pair's total equals the required total. The audit stops the moment a pair's total differs from the required total, or when fewer than two entries remain unsettled.
Determine how many settlements you complete before stopping.
Line 1: a single integer n, the number of transaction amounts.
Line 2: n space-separated integers amount_1 ... amount_n.
Print a single integer: the number of settlements completed.
Example 1
Input
5 3 2 1 4 5
Expected
2
Explanation
The first settlement pairs 3 and 2, total 5 -- this fixes the required total at 5. The next settlement pairs 1 and 4, total 5, which matches, so it counts as a second settlement. Only one entry (5) remains, too few to form another pair, so the audit stops. Two settlements were completed.
Example 2
Input
5 3 2 6 1 4
Expected
1
Explanation
The first settlement pairs 3 and 2, total 5, fixing the required total at 5. The next candidate pair is 6 and 1, total 7, which does not match 5, so the audit stops immediately after the first settlement. Only one settlement was completed.
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 →