Open In App

Minimize steps to obtain N from M by adding M/X in each step

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to find the minimum number of steps to obtain N from M (M = 1 initially). In each step, M/X can be added to M where X is any positive integer. 

Examples:

Input: N = 5
Output: 3
Explanation: Initially the number is 1. 1/1 is added to make it 2.
In next step adding 2/1 = 2 it becomes 4. At last add 4/4 = 1 to get the 5. 
This is the minimum steps required to convert 1 to 5

Input: N = 7
Output: 4
Explanation: Initially the number is 1. 
Now 1/1 is added to it and it becomes 2. 
After adding 2/1 = 2 it becomes 4. In the third 4/2 = 2 is added and it becomes 6.
At the final step add 6/6 = 1 and it becomes 7.

 

Approach: The approach of this question is using Dynamic Programming. For each integer, there are many possible moves. Store the minimum steps required to reach every number in the dp[] array and use this for the next numbers. Follow the steps mentioned below.

  • Start iterating from 2 to N.
  • For each number i, do the following:
    • Check from which numbers (less than i), we can reach i.
    • Now for those numbers find the minimum steps required to reach i by using the relation dp[i] = min(dp[i], dp[j]+1) where j is such a number from where i can be reached.
    • Store that minimum value in dp[i] array.
  • After iteration is done for all elements up to N return value of dp[N].

Below is the implementation of the above approach: 

C++




// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum steps required
int minSteps(int N)
{
  vector<int> dp(N + 1, INT_MAX);
  dp[1] = 0;
 
  // Loop to find the minimum steps to
  // reach N from 1
  for (int i = 2; i <= N; ++i) {
    for (int j = 1; j <= i; ++j) {
 
      // Finding the distance
      // between two numbers
      int distance = i - j;
      if (distance == 0) {
        continue;
      }
 
      // Divide the number
      int divide = j / distance;
      if (divide != 0) {
 
        // Checking if the number
        // can be reached or not
        if (j / divide == distance) {
          dp[i] = min(dp[j] + 1, dp[i]);
        }
      }
    }
  }
  return dp[N];
}
 
// Driver code
int main()
{
  int N = 7;
 
  int ans = minSteps(N);
  cout << (ans);
 
  return 0;
}
 
// This code is contributed by rakeshsahni


Java




// Java code to implement above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum steps required
    static int minSteps(int N)
    {
        int dp[] = new int[N + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[1] = 0;
 
        // Loop to find the minimum steps to
        // reach N from 1
        for (int i = 2; i <= N; ++i) {
            for (int j = 1; j <= i; ++j) {
 
                // Finding the distance
                // between two numbers
                int distance = i - j;
                if (distance == 0) {
                    continue;
                }
 
                // Divide the number
                int divide = j / distance;
                if (divide != 0) {
 
                    // Checking if the number
                    // can be reached or not
                    if (j / divide == distance) {
                        dp[i]
                            = Math.min(dp[j] + 1,
                                       dp[i]);
                    }
                }
            }
        }
        return dp[N];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 7;
 
        int ans = minSteps(N);
        System.out.println(ans);
    }
}


Python




# Python] code to implement above approach
import sys
 
# Function to find the minimum steps required
def minSteps(N):
     
  dp = []
  dp = [sys.maxsize for i in range(N + 1)]
  dp[1] = 0;
 
  # Loop to find the minimum steps to
  # reach N from 1
  for i in range(2, N + 1):
    for j in range(1, i + 1):
 
      # Finding the distance
      # between two numbers
      distance = i - j
      if (distance == 0):
        continue
 
      # Divide the number
      divide = j // distance;
      if (divide != 0):
 
        # Checking if the number
        # can be reached or not
        if (j // divide == distance):
          dp[i] = min(dp[j] + 1, dp[i])
           
  return dp[N]
 
# Driver code
 
N = 7
 
ans = minSteps(N);
print(ans)
 
# This code is contributed by Samim Hossain Mondal.


C#




// C# program for the above approach
using System;
 
public class GFG{
   
    // Function to find the minimum steps required
    static int minSteps(int N)
    {
        int[] dp = new int[N + 1];
          for(int i = 0; i < N + 1; i++)
            dp[i] = Int32.MaxValue;
       
        dp[1] = 0;
 
        // Loop to find the minimum steps to
        // reach N from 1
        for (int i = 2; i <= N; ++i) {
            for (int j = 1; j <= i; ++j) {
 
                // Finding the distance
                // between two numbers
                int distance = i - j;
                if (distance == 0) {
                    continue;
                }
 
                // Divide the number
                int divide = j / distance;
                if (divide != 0) {
 
                    // Checking if the number
                    // can be reached or not
                    if (j / divide == distance) {
                        dp[i]
                            = Math.Min(dp[j] + 1,
                                       dp[i]);
                    }
                }
            }
        }
        return dp[N];
    }
 
    // Driver code
    static public void Main (){
 
        int N = 7;
 
        int ans = minSteps(N);
        Console.Write(ans);
    }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
// JavaScript code for the above approach
 
// Function to find the minimum steps required
function minSteps( N)
{
  let dp = new Array(N + 1).fill(Number.MAX_VALUE);
  dp[1] = 0;
 
  // Loop to find the minimum steps to
  // reach N from 1
  for (let i = 2; i <= N; ++i) {
    for (let j = 1; j <= i; ++j) {
 
      // Finding the distance
      // between two numbers
      let distance = i - j;
      if (distance == 0) {
        continue;
      }
 
      // Divide the number
      let divide =Math.floor(j / distance);
      if (divide != 0) {
 
        // Checking if the number
        // can be reached or not
        if (j / divide == distance) {
          dp[i] = Math.min(dp[j] + 1, dp[i]);
        }
      }
    }
  }
  return dp[N];
}
 
// Driver code
  let N = 7;
 
  let ans = minSteps(N);
  document.write((ans));
 
// This code is contributed by Potta Lokesh
    </script>


 
 

Output

4

 

Time Complexity: O(N*N)
Auxiliary Space: O(N)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads