Here, we will discuss the solution to Javelin from AIO 2024.
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?
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:
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);
}