How I approached and finally solved the problem “OIE Excursion.”
Hi, I am Afridi. Today I want to discuss how I approached and finally solved the problem “OIE Excursion”. I am not going to jump straight to the final trick. Instead, I’ll walk through the exact thought process, including the wrong ideas and corrections, because that’s where the real understanding comes from.
Let’s try to Understanding the Problem first: Hector needs to pass through n guards.Each guard:
- Has a timer starting at
a[i] - Every second it increases by 1
- When it reaches
m, it resets to 0 - The guard is dangerous only when timer = 0
So at time x, guard i is dangerous if:
(a[i] + x) % m == 0Okay; First Simplification Rewriting the condition:
x % m == (m - a[i]) % mLet:
danger[i] = (m - a[i]) % mNow the meaning is simple: At position i, Hector must avoid times where:
time % m == danger[i]Each guard blocks exactly one value modulo m.
My first thought was (Which was wrong):
Hector can wait at the beginning, choose a delay
d, and then just walk.
So he reaches position i at time:
i + dThen the condition becomes:
(i + d) % m != danger[i]Which leads to:
d % m != (danger[i] - i) % mSo each guard bans one value of d. Then the idea becomes:
- There are
mpossible values ofd - Each guard blocks one
- If not all are blocked → answer is YES
This looked clean, but it failed on test cases.
but my question was why That Idea Fails on test cases? when code looks fine?Because Hector is not restricted to a single global delay.
He can:
- Wait at different positions
- Adjust timing step by step
So his delay is not fixed. This completely breaks the “single d” model.
My second Realization wasHector can wait, but not infinitely.
If he stays too long at a guard:
- Eventually the timer cycles back to 0
- He gets caught
Since the cycle is m, the maximum safe waiting is:
m - 1 secondsSo at each position:
- He can shift his time
- But only within a window of size
m
This is the real constraint.
Get Afridi Akbar Ifty’s stories in your inbox
Join Medium for free to get updates from this writer.
okay, now let’s Reframing the Problem, each guard forbids one residue:
time % m == danger[i]Hector tries to move from position i to i+1, adjusting time slightly.
At each step:
- He can increase time by 1 (move)
- Or wait up to
m-1seconds
So he has flexibility, but limited.
After experimenting and thinking through transitions, the pattern becomes clear:
Hector only gets stuck if there is a long sequence of guards that all block the same time.
More precisely:
If there are m consecutive positions where:
danger[i] == danger[i+1] == ... == same valueThen Hector cannot pass.
Why This Condition? Because:
- Each guard blocks the same residue
- Hector can shift his time by at most
m-1 - But to escape
midentical blocks, he would need a shift of at leastm
Which is impossible.
So he gets trapped. so the Final Condition is
- If there exists a segment of length at least
mwhere alldanger[i]are equal → NO - Otherwise → YES
Example
Consider:
n = 6, m = 2
a = [1, 1, 1, 1, 0, 1]Compute:
danger[i] = (2 - a[i]) % 2
= [1, 1, 1, 1, 0, 1]There are 4 consecutive 1s.
Since m = 2, even 2 consecutive equal values are enough.
So the answer is:
NOImplementation :
We just scan the array and track consecutive equal danger[i].
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long m = Long.parseLong(st.nextToken());
long[] a = new long[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(st.nextToken());
}
boolean impossible = false;
long run = 1;
long prev = (m - a[0]) % m;
for (int i = 1; i < n; i++) {
long curr = (m - a[i]) % m;
if (curr == prev) {
run++;
if (run >= m) {
impossible = true;
break;
}
} else {
run = 1;
prev = curr;
}
}
sb.append(impossible ? "NO\n" : "YES\n");
}
System.out.print(sb);
}
}The problem looks like simulation, but it is actually about recognizing constraints on timing.
The key steps were:
- Convserting time into modulo form
- Realizing waiting is limited
- Identifying when that limitation becomes critical
Once that pattern is clear, the solution becomes straightforward.
If you are practicing problem solving, this is a good reminder that wrong approaches are not wasted time. They often lead directly to the correct insight.









