Problem 1 - Soccer Match

We will discuss the solution to Soccer Match from AIO 2025.

Solution

We consider the goals one at a time while storing the current score for each team. After each goal, we should check whether our team is winning. This solution has a time complexity of O(N) which is fast enough to score 100 in this problem.

Code

We can code this solution using a for loop and some if statements. Below you can see this solution written in Python and C++. I suggest trying to code the solution yourself and only look at our code after you finish or if you get stuck.

# N is the number of goals.
N = 0

# G contains the goals. Note that the list starts from 0, and so the values are
# G[0] to G[N-1].
G = []

# Read the value of N and the sequence of goals.
N = int(input().strip())
G = list(map(int, input().strip().split()))

# Compute the answer
team_1_score = 0
team_2_score = 0
answer = False

for g in G:
  if g == 1:
    team_1_score += 1
  else:
    team_2_score += 1
  
  if team_1_score > team_2_score:
    # We can stop watching the game now!
    answer = True

if answer:
  print("YES")
else:
  print("NO")
#include <cstdio>

/* N is the number of goals. */
int N;

/*
 * G contains the goals. Note that the array starts from 0, and so the values
 * are G[0] to G[N-1].
 */
int G[200005];

int main(void) {
  /* Read the value of N and the sequence of goals. */
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &G[i]);
  }

  /* Compute the answer */
  int team_1_score = 0;
  int team_2_score = 0;
  bool answer = false;

  for (int i = 0; i < N; i++) {
    if (G[i] == 1) {
      team_1_score++;
    } else {
      team_2_score++;
    }

    if (team_1_score > team_2_score) {
      // We can stop watching the game now!
      answer = true;
    }
  }

  if (answer) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }
}

Click here to go back.