This problem is about two baseball teams, but the algorithmic idea is a classic running prefix sum. We compare the two teams’ cumulative scores after each day and remember the last day when those totals are equal.
The important detail is that we are not comparing the scores from a single day. We are comparing the total scores from day 1 through day K.
Where This Fits in the CCC DSA Map
- Level: Senior S1
- Pattern: Running Prefix Sum, Last Valid Position
- Prerequisite: arrays/lists, loops, accumulation variables, indexing, careful output convention
- What this teaches: You do not always need a full prefix array; when only the current cumulative state matters, rolling totals are enough.
- Where it appears again: prefix sums, range sums, two-array comparisons, sweep-line style accumulation
What to Study Next
- Related CP Weekly problems: CCC 2025 J4 Sunny Days for prefix/suffix consecutive counts, CCC 2021 S1 Crazy Fencing for array accumulation (coming soon), CCC 2020 S3 Searching for Strings for sliding window (coming soon).
- Related Python / C++ foundations: loops and accumulation, lists/vectors, indexing, integer totals.
- Related DSA roadmap article: CCC Prefix and Sliding Window Patterns (planned).
What the Problem Is Asking
The season lasts for N days. The Swifts and the Semaphores each play one game per day.
The input gives:
N
daily scores for the Swifts
daily scores for the Semaphores
We need to output the largest integer K such that after K days:
Swifts total score == Semaphores total score
If there is no such day after the season starts, we output 0.
The teams are tied before any games are played, but that does not count as a positive K.
Where Beginners Often Get Stuck
The first common mistake is comparing daily scores instead of cumulative scores. The problem asks whether the two teams have the same total after K days.
The second common mistake is stopping at the first equal total. The problem asks for the largest K, so we must scan all days and keep updating the answer.
The third common mistake is forgetting that the answer can be 0.
Core Algorithm Idea
Maintain two running totals:
swift_total
semaphore_total
For each day, add both teams’ scores to their totals. If the totals are equal, record the current day as the answer.
Because we scan days from left to right, the last recorded equal day is automatically the largest valid K.
Python Solution
N = int(input())
swift_scores = list(map(int, input().split()))
semaphore_scores = list(map(int, input().split()))
swift_total = 0
semaphore_total = 0
answer = 0
for i in range(N):
swift_total += swift_scores[i]
semaphore_total += semaphore_scores[i]
if swift_total == semaphore_total:
answer = i + 1
print(answer)
C++ Solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> swift(N);
vector<int> semaphore(N);
for (int i = 0; i < N; i++) {
cin >> swift[i];
}
for (int i = 0; i < N; i++) {
cin >> semaphore[i];
}
int swiftTotal = 0;
int semaphoreTotal = 0;
int answer = 0;
for (int i = 0; i < N; i++) {
swiftTotal += swift[i];
semaphoreTotal += semaphore[i];
if (swiftTotal == semaphoreTotal) {
answer = i + 1;
}
}
cout << answer << "\n";
return 0;
}
Complexity Analysis
We scan the N days once.
The time complexity is:
O(N)
The shown implementation stores the two score arrays, so the memory complexity is:
O(N)
The idea can also be implemented with O(1) extra memory if the input is streamed or processed in a different format.
Common Mistakes
- Comparing daily scores instead of cumulative scores.
- Stopping at the first equal cumulative total.
- Forgetting that the answer can be
0. - Using
iinstead ofi + 1for the day number. - Mixing up prefix-array indices with day numbers.
Review Questions
- Why do we need cumulative totals in this problem?
- Why should we continue scanning after finding an equal day?
- Why is
answerinitialized to0? - Why does
i + 1represent the current day? - When would a full prefix sum array be more useful than two running totals?
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
