Problem 1 - Javelin

Here, we will discuss the solution to Javelin from AIO 2024.

Summary

N students participated in a javelin throwing event. A student is a current leader if they threw further than everyone before them. How many different current leaders were there during the competition?

Solution

We consider the students one at a time while storing the furthest distance that a student has thrown so far (we will call this value best_throw). There are two cases:

  1. If Di > best_throw, then student i is a current winner. The best_throw is now Di, and we should add 1 to the answer.
  2. Otherwise, student i is not a current winner.
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 an if statement. 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.

# Read the value of N and the distances.
N = int(input().strip())
D = list(map(int, input().strip().split()))

# Compute the answer
answer = 0
best_throw = 0
for d in D:
    if d > best_throw:
    	# Student i is the current winner
        best_throw = d
        answer += 1

# Write the answer.
print(answer)
#include <cstdio>
int N;
int D[200005];
int main(void) {
    /* Read the value of N and the distances. */
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        scanf("%d", &D[i]);
    }

    // Compute the answer
    int best_throw = 0;
    int answer = 0;
    for (int i = 0; i < N; i++) {
        if (D[i] > best_throw) {
            // Student i is the current winner
            best_throw = D[i];
            answer++;
        }
    }

    /* Write the answer. */
    printf("%d\n", answer);
}

Click here to go back.