Minimize steps to obtain N from M by adding M/X in each step
Last Updated :
28 Jul, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int minSteps( int N)
{
vector< int > dp(N + 1, INT_MAX);
dp[1] = 0;
for ( int i = 2; i <= N; ++i) {
for ( int j = 1; j <= i; ++j) {
int distance = i - j;
if (distance == 0) {
continue ;
}
int divide = j / distance;
if (divide != 0) {
if (j / divide == distance) {
dp[i] = min(dp[j] + 1, dp[i]);
}
}
}
}
return dp[N];
}
int main()
{
int N = 7;
int ans = minSteps(N);
cout << (ans);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int minSteps( int N)
{
int dp[] = new int [N + 1 ];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[ 1 ] = 0 ;
for ( int i = 2 ; i <= N; ++i) {
for ( int j = 1 ; j <= i; ++j) {
int distance = i - j;
if (distance == 0 ) {
continue ;
}
int divide = j / distance;
if (divide != 0 ) {
if (j / divide == distance) {
dp[i]
= Math.min(dp[j] + 1 ,
dp[i]);
}
}
}
}
return dp[N];
}
public static void main(String[] args)
{
int N = 7 ;
int ans = minSteps(N);
System.out.println(ans);
}
}
|
Python
import sys
def minSteps(N):
dp = []
dp = [sys.maxsize for i in range (N + 1 )]
dp[ 1 ] = 0 ;
for i in range ( 2 , N + 1 ):
for j in range ( 1 , i + 1 ):
distance = i - j
if (distance = = 0 ):
continue
divide = j / / distance;
if (divide ! = 0 ):
if (j / / divide = = distance):
dp[i] = min (dp[j] + 1 , dp[i])
return dp[N]
N = 7
ans = minSteps(N);
print (ans)
|
C#
using System;
public class GFG{
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;
for ( int i = 2; i <= N; ++i) {
for ( int j = 1; j <= i; ++j) {
int distance = i - j;
if (distance == 0) {
continue ;
}
int divide = j / distance;
if (divide != 0) {
if (j / divide == distance) {
dp[i]
= Math.Min(dp[j] + 1,
dp[i]);
}
}
}
}
return dp[N];
}
static public void Main (){
int N = 7;
int ans = minSteps(N);
Console.Write(ans);
}
}
|
Javascript
<script>
function minSteps( N)
{
let dp = new Array(N + 1).fill(Number.MAX_VALUE);
dp[1] = 0;
for (let i = 2; i <= N; ++i) {
for (let j = 1; j <= i; ++j) {
let distance = i - j;
if (distance == 0) {
continue ;
}
let divide =Math.floor(j / distance);
if (divide != 0) {
if (j / divide == distance) {
dp[i] = Math.min(dp[j] + 1, dp[i]);
}
}
}
}
return dp[N];
}
let N = 7;
let ans = minSteps(N);
document.write((ans));
</script>
|
Time Complexity: O(N*N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...