Open In App

Minimum steps to reach a destination

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an infinite number line. We start at 0 and can go either to the left or to the right. The condition is that in the ith move, you must take i steps. Given a destination d, the task is to find the minimum number of steps required to reach that destination.

Examples:

Input: d = 2
Output: 3
Explaination: The steps taken are +1, -2 and +3.

Input: d = 10
Output: 4
Explaination: The steps taken are +1, +2, +3 and +4.

Important Points:

  • We can always reach any destination as it is always possible to make a move of length 1. At any step i, we can move forward i, then backward i + 1.
  • Since the distance of +5 and -5 from 0 is the same, hence we find the answer for the absolute value of the destination.

Approach:

The approach to solving the minimum steps problem is based on recursive, where we explore both left and right movements on a number line. Starting from the origin (0), we recursively explore both movements, incrementing the number of steps at each iteration. The function keeps track of the current position and compares it with the destination. If the absolute value of the current position exceeds the destination, the path is considered invalid, and the function returns a large value. If the current position matches the destination, the number of steps taken is returned.

C++
// C++ program to find Minimum steps to reach a destination
// using recursion
#include <bits/stdc++.h>
using namespace std;

int minRecur(int curr, int steps, int d) {
    
    // This ensures this path is not 
    // considered for minimum steps
    if (abs(curr) > d) return INT_MAX;
    
    if (curr == d) return steps;
    
    // Recursively explore left movement
    int left = minRecur(curr-steps-1, steps+1, d);
    
    // Recursively explore right movement
    int right = minRecur(curr+steps+1, steps+1, d);
    
    // Return the minimum steps path 
    // between left and right movements
    return min(left, right);
}

int minSteps(int d) {

    // Convert destination to positive distance
    d = abs(d);
    return minRecur(0, 0, d);
}

int main() {
    int d = 10;
    cout << minSteps(d);
    return 0;
}
Java
// Java program to find Minimum steps to
// reach a destination using recursion
import java.lang.Math;

class GfG {
    static int minRecur(int curr, int steps, int d) {
        
        // This ensures this path is not 
        // considered for minimum steps
        if (Math.abs(curr) > d) return Integer.MAX_VALUE;
        
        if (curr == d) return steps;
        
        // Recursively explore left movement
        int left = minRecur(curr - steps - 1, steps + 1, d);
        
        // Recursively explore right movement
        int right = minRecur(curr + steps + 1, steps + 1, d);
        
        // Return the minimum steps path 
        // between left and right movements
        return Math.min(left, right);
    }

    static int minSteps(int d) {

        // Convert destination to positive distance
        d = Math.abs(d);
        return minRecur(0, 0, d);
    }

    public static void main(String[] args) {
        int d = 10;
        System.out.println(minSteps(d));
    }
}
Python
# Python program to find Minimum steps to reach a destination
# using recursion
import sys

def minRecur(curr, steps, d):
    
    # This ensures this path is not 
    # considered for minimum steps
    if abs(curr) > d:
        return sys.maxsize
    
    if curr == d:
        return steps
    
    # Recursively explore left movement
    left = minRecur(curr - steps - 1, steps + 1, d)
    
    # Recursively explore right movement
    right = minRecur(curr + steps + 1, steps + 1, d)
    
    # Return the minimum steps path 
    # between left and right movements
    return min(left, right)

def minSteps(d):
    
    # Convert destination to positive distance
    d = abs(d)
    return minRecur(0, 0, d)

if __name__ == "__main__":
    d = 10
    print(minSteps(d))
C#
// C# program to find Minimum steps to reach a destination
// using recursion
using System;

class GfG {
    static int minRecur(int curr, int steps, int d) {
        
        // This ensures this path is not 
        // considered for minimum steps
        if (Math.Abs(curr) > d) return int.MaxValue;
        
        if (curr == d) return steps;
        
        // Recursively explore left movement
        int left = minRecur(curr - steps - 1, steps + 1, d);
        
        // Recursively explore right movement
        int right = minRecur(curr + steps + 1, steps + 1, d);
        
        // Return the minimum steps path 
        // between left and right movements
        return Math.Min(left, right);
    }

    static int minSteps(int d) {
        
        // Convert destination to positive distance
        d = Math.Abs(d);
        return minRecur(0, 0, d);
    }

    static void Main(string[] args) {
        int d = 10;
        Console.WriteLine(minSteps(d));
    }
}
JavaScript
// JavaScript program to find Minimum steps to reach a destination
// using recursion
function minRecur(curr, steps, d) {
    
    // This ensures this path is not 
    // considered for minimum steps
    if (Math.abs(curr) > d) return Number.MAX_SAFE_INTEGER;
    
    if (curr === d) return steps;
    
    // Recursively explore left movement
    const left = minRecur(curr - steps - 1, steps + 1, d);
    
    // Recursively explore right movement
    const right = minRecur(curr + steps + 1, steps + 1, d);
    
    // Return the minimum steps path 
    // between left and right movements
    return Math.min(left, right);
}

function minSteps(d) {
    
    // Convert destination to positive distance
    d = Math.abs(d);
    return minRecur(0, 0, d);
}

const d = 10;
console.log(minSteps(d));

Output
4

Time Complexity: O(2^n)
Auxiliary Space: O(d), due to the recursion depth

Please refer to Find minimum moves to reach target on an infinite line for optimised solution.


Similar Reads