❓ The question first
❓ THE QUESTION
Problem — Sum Game
Annie tracked her two baseball teams, the Swifts and the Semaphores, for N days; both played one game per day. Find the largest K (K ≤ N) such that after K days, both teams had scored the same total number of runs. If they were never tied after any day, output 0.
Input
First line: N. Second line: N integers — Swifts runs per day. Third line: N integers — Semaphores runs per day.
3 1 3 3 2 2 6
Output
One integer: the largest K with equal totals, or 0.
2
🎬 The solution, step by step
The idea — two running totals, one notebook:
- Walk the days once, keeping each team's cumulative total (a prefix sum again — same trick as Are We There Yet? (2018)).
- After each day, compare the two totals. If equal, write the day down, overwriting the previous one.
- Whatever day is in the notebook at the end is the answer (0 if it is empty).
Animation — walk the official sample
💻 The code
N = int(input())
swifts = list(map(int, input().split()))
semaphores = list(map(int, input().split()))
K = 0
total_a = 0
total_b = 0
for day in range(N):
total_a += swifts[day] # running totals,
total_b += semaphores[day] # never daily scores
if total_a == total_b:
K = day + 1 # overwrite: the LATEST tie wins
print(K)
⭐ Key points & traps
Trap 1 — comparing daily scores: the question is about totals after K days, not about who scored more on day K. Two running totals, compared after every day.
Trap 2 — stopping at the first tie: the question asks for the largest K. Sample 3 ties after days 1, 3 and 4; a program that breaks early prints 1 and scores 0. Overwrite, don't stop.
Day counting: days are counted from 1 in the answer but from 0 in the loop — hence
K = day + 1. Off-by-one here is the cheapest mistake in the whole problem.✋ Your turn — predict first, then check
Sample 3 by hand: N = 4, Swifts 1 2 3 4, Semaphores 1 3 2 4. Answer?
Running totals — day 1: 1 = 1 ✔ (K=1); day 2: 3 vs 4 ✗; day 3: 6 = 6 ✔ (K=3); day 4: 10 = 10 ✔ (K=4). Largest: 4.