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:
arrdFrom index i, you can jump:
But only up to distance d.
You can jump from index i to index j only if:
|i - j| <= d
arr[i] > arr[j]
All elements between i and j
must also be smaller than arr[i].
If a taller or equal element appears:
๐ซ Jump becomes blocked.
We can start from ANY index.
Return:
Maximum number of indices we can visit.
For every index:
We want to know:
"What is the maximum path length starting from this index?"
This is a DP problem.
dp[i]
means:
Maximum indices we can visit starting from index i.
From every index:
Try all valid jumps:
Take maximum answer among all paths.
Each index explores:
This naturally forms:
DFS + Memoization
If answer for an index is already calculated:
No need to calculate again.
Store result in:
dp[i]
For every valid next index:
answer = 1 + dfs(nextIndex)
Take maximum among all choices.
While moving left or right:
If we encounter:
arr[next] >= arr[current]
Immediately stop exploring further.
Because jump gets blocked.
arr = [6,4,14,6,8,13,9,7,10,6,12]
d = 2
Index:
0 1 2 3 4 5 6 7 8 9 10
Value:
6 4 14 6 8 13 9 7 10 6 12
Current value:
12
Possible jumps within distance 2:
Both are smaller than 12.
So we explore both.
Current value:
10
Possible jumps:
Explore both.
Current value:
9
Possible jumps:
Index 5:
13
Blocked โ
Because:
13 > 9
Cannot move further left.
Current value:
7
Left:
9 > 7
Blocked โ
Right:
10 > 7
Blocked โ
No valid jumps.
So:
dp[7] = 1
Now:
dp[6] = 1 + dp[7]
= 2
Similarly:
dp[8] = 1 + max(dp[6], dp[7])
= 3
Best path:
10 โ 8 โ 6 โ 7
Visited indices:
4
Answer:
4
class Main {
static int[] dp;
public static int dfs(int[] arr, int d, int index) {
// Already calculated
if (dp[index] != 0) {
return dp[index];
}
int max = 1;
// Move Right
for (int i = index + 1; i <= Math.min(index + d, arr.length - 1); i++) {
// Blocked
if (arr[i] >= arr[index]) {
break;
}
max = Math.max(max, 1 + dfs(arr, d, i));
}
// Move Left
for (int i = index - 1; i >= Math.max(index - d, 0); i--) {
// Blocked
if (arr[i] >= arr[index]) {
break;
}
max = Math.max(max, 1 + dfs(arr, d, i));
}
return dp[index] = max;
}
public static int maxJumps(int[] arr, int d) {
int n = arr.length;
dp = new int[n];
int answer = 1;
for (int i = 0; i < n; i++) {
answer = Math.max(answer, dfs(arr, d, i));
}
return answer;
}
public static void main(String[] args) {
int[] arr = {6,4,14,6,8,13,9,7,10,6,12};
int d = 2;
System.out.println(
maxJumps(arr, d)
);
}
}
O(N ร d)
Because each index explores at most:
O(N)
Used for:
Key Learnings:
This combination of:
DFS + DP
gives optimized solution efficiently.
โ 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.