How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
You are given:
sminJumpmaxJumpYou start from index 0.
You can jump from index i to index j if:
i + minJump <= j <= i + maxJump
and
s[j] == '0'
Return:
true โ if you can reach last indexfalse โ otherwiseFor every position:
We only need to know:
"Is there ANY reachable index from which I can jump here?"
If YES:
For every index:
This becomes slow.
We use:
dp[i]
Meaning:
Can we reach index i ?
Suppose we are at index:
i
Then valid previous positions are:
[i - maxJump ... i - minJump]
If ANY index inside this range is reachable:
Then current index is also reachable.
Instead of checking whole range every time:
We maintain:
reachableCount
Meaning:
How many reachable indices currently exist inside valid range
As window moves:
When new index enters range:
i - minJump
If reachable:
reachableCount++
When old index leaves range:
i - maxJump - 1
If reachable:
reachableCount--
If:
reachableCount > 0
AND
s[i] == '0'
Then:
dp[i] = true
s = "011010"
minJump = 2
maxJump = 3
Start:
dp = [T, F, F, F, F, F]
Valid previous range:
[0 ... 0]
Index 0 reachable โ
But:
s[2] = '1'
So cannot stand here.
Valid range:
[0 ... 1]
Index 0 reachable โ
And:
s[3] = '0'
So:
dp[3] = true
Valid range:
[2 ... 3]
Index 3 reachable โ
And:
s[5] = '0'
So:
dp[5] = true
Reached end ๐
[T, F, F, T, F, T]
Answer:
true
class Main {
public static boolean canReach(String s, int minJump, int maxJump) {
int n = s.length();
boolean[] dp = new boolean[n];
dp[0] = true;
int reachableCount = 0;
for (int i = 1; i < n; i++) {
// Add new reachable index into window
if (i - minJump >= 0 && dp[i - minJump]) {
reachableCount++;
}
// Remove old index from window
if (i - maxJump - 1 >= 0 && dp[i - maxJump - 1]) {
reachableCount--;
}
// Current position reachable
if (s.charAt(i) == '0' && reachableCount > 0) {
dp[i] = true;
}
}
return dp[n - 1];
}
public static void main(String[] args) {
String s = "011010";
int minJump = 2;
int maxJump = 3;
System.out.println(
canReach(s, minJump, maxJump)
);
}
}
O(N)
Because every index is processed once.
O(N)
For DP array.
โ Written by Saurabh Patil โข B.Tech CSE โข Software Developer๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.