The Spider Steps

Last Updated : 23 Jul, 2026

Given three integers hu, and d, representing the height of a well, the distance a spider climbs in each step, and the distance it slips after each step, respectively.

  • In every step, the spider first climbs u units.
  • If the spider reaches or exceeds the top of the well after climbing, it escapes immediately and does not slip back.
  • Otherwise, it slips down by d units.

Return the minimum number of steps required for the spider to escape. If it is impossible for the spider to escape, return -1.

Examples:

Input: h = 200, u = 50, d = 1
Output: 5
Explanation: Step 1: 0 → 50 → 49
Step 2: 49 -> 99 -> 98
Step 3: 98 -> 148 -> 147
Step 4: 147 -> 197 -> 196
Step 5: 196 -> 246 (escapes)

Input: h = 100, u = 5, d = 3
Output: 49
Explanation: After each completed step, the spider makes a net gain of u − d = 2 units. After 48 steps, it is at height 96. On the 49th step, it climbs from 96 to 101, reaches the top of the well, and escapes without slipping back.

Try It Yourself
redirect icon

[Naive Approach] Step by Step Simulation - O(steps) Time and O(1) Space

Simulate spider's movement step by step. In each step, it climbs up, checks if it reached top, then slips down. Track number of steps.

C++
#include <iostream>

using namespace std;

int minStep(int h, int u, int d) {

    int currHeight = 0;
    int steps = 0;

    while (currHeight < h) {

        steps++;

        // Spider climbs
        currHeight += u;

        // Spider escaped
        if (currHeight >= h) {
            return steps;
        }

        // Spider slips
        currHeight -= d;

        // Impossible to escape
        if (u <= d) {
            return -1;
        }
    }

    return steps;
}

int main() {

    int h = 10;
    int u = 3;
    int d = 1;

    cout << minStep(h, u, d);

    return 0;
}
Java
import java.util.*;

class GfG {
    
    static int minStep(int h, int u, int d) {
        int currHeight = 0;
        int steps = 0;
        
        while (currHeight < h) {
            steps++;
            
            // Spider climbs
            currHeight += u;
            
            // Spider escaped
            if (currHeight >= h) {
                return steps;
            }
            
            // Spider slips
            currHeight -= d;
            
            // Impossible to escape
            if (u <= d) {
                return -1;
            }
        }
        
        return steps;
    }
    
    public static void main(String[] args) {
        int h = 10;
        int u = 3;
        int d = 1;
        
        System.out.println(minStep(h, u, d));
    }
}
Python
def minStep(h, u, d):
    currHeight = 0
    steps = 0
    
    while currHeight < h:
        steps += 1
        
        # Spider climbs
        currHeight += u
        
        # Spider escaped
        if currHeight >= h:
            return steps
        
        # Spider slips
        currHeight -= d
        
        # Impossible to escape
        if u <= d:
            return -1
    
    return steps

if __name__ == "__main__":
    h = 10
    u = 3
    d = 1
    
    print(minStep(h, u, d))
C#
using System;

class GfG {
    
    static int minStep(int h, int u, int d) {
        int currHeight = 0;
        int steps = 0;
        
        while (currHeight < h) {
            steps++;
            
            // Spider climbs
            currHeight += u;
            
            // Spider escaped
            if (currHeight >= h) {
                return steps;
            }
            
            // Spider slips
            currHeight -= d;
            
            // Impossible to escape
            if (u <= d) {
                return -1;
            }
        }
        
        return steps;
    }
    
    static void Main(string[] args) {
        int h = 10;
        int u = 3;
        int d = 1;
        
        Console.WriteLine(minStep(h, u, d));
    }
}
JavaScript
function minStep(h, u, d) {
    let currHeight = 0;
    let steps = 0;
    
    while (currHeight < h) {
        steps++;
        
        // Spider climbs
        currHeight += u;
        
        // Spider escaped
        if (currHeight >= h) {
            return steps;
        }
        
        // Spider slips
        currHeight -= d;
        
        // Impossible to escape
        if (u <= d) {
            return -1;
        }
    }
    
    return steps;
}

const h = 10;
const u = 3;
const d = 1;

console.log(minStep(h, u, d));

Output
5

[Expected Approach] Mathematical Formula - O(1) Time and O(1) Space

Each complete step (climb + slip) gains u - d units.

The last step is only a climb (no slip), so before the final step the spider needs to be below h by at most u units.

Number of full steps are needed to reach at least h - u is, fullSteps = ceil((h - u)/(u - d))

The answer is fullSteps + 1. [1 is added for the last step]

Corner Cases:

  • If the spider can reach the top in the first climb (u >= h), the answer is 1.
  • If the spider slides back as much as or more than it climbs (u <= d), it will never make progress, so return -1.
C++
#include <iostream>

using namespace std;

int minStep(int h, int u, int d) {

    // Escapes in first step
    if (u >= h) {
        return 1;
    }

    // Impossible to escape
    if (u <= d) {
        return -1;
    }

    int netGain = u - d;

    int remaining = h - u;

    int fullSteps = (remaining + netGain - 1) / netGain;

    return fullSteps + 1;
}

int main() {

    int h = 10;
    int u = 3;
    int d = 1;

    cout << minStep(h, u, d);

    return 0;
}
Java
import java.util.*;

class GfG {
    
    static int minStep(int h, int u, int d) {
        // Escapes in first step
        if (u >= h) {
            return 1;
        }
        
        // Impossible to escape
        if (u <= d) {
            return -1;
        }
        
        int netGain = u - d;
        int remaining = h - u;
        int fullSteps = (remaining + netGain - 1) / netGain;
        
        return fullSteps + 1;
    }
    
    public static void main(String[] args) {
        int h = 10;
        int u = 3;
        int d = 1;
        
        System.out.println(minStep(h, u, d));
    }
}
Python
def minStep(h, u, d):
    # Escapes in first step
    if u >= h:
        return 1
    
    # Impossible to escape
    if u <= d:
        return -1
    
    netGain = u - d
    remaining = h - u
    fullSteps = (remaining + netGain - 1) // netGain
    
    return fullSteps + 1

if __name__ == "__main__":
    h = 10
    u = 3
    d = 1
    
    print(minStep(h, u, d))
C#
using System;

class GfG {
    
    static int minStep(int h, int u, int d) {
        // Escapes in first step
        if (u >= h) {
            return 1;
        }
        
        // Impossible to escape
        if (u <= d) {
            return -1;
        }
        
        int netGain = u - d;
        int remaining = h - u;
        int fullSteps = (remaining + netGain - 1) / netGain;
        
        return fullSteps + 1;
    }
    
    static void Main(string[] args) {
        int h = 10;
        int u = 3;
        int d = 1;
        
        Console.WriteLine(minStep(h, u, d));
    }
}
JavaScript
function minStep(h, u, d) {
    // Escapes in first step
    if (u >= h) {
        return 1;
    }
    
    // Impossible to escape
    if (u <= d) {
        return -1;
    }
    
    let netGain = u - d;
    let remaining = h - u;
    let fullSteps = Math.floor((remaining + netGain - 1) / netGain);
    
    return fullSteps + 1;
}

const h = 10;
const u = 3;
const d = 1;

console.log(minStep(h, u, d));

Output
5
Comment