Count of ways to convert 2 into N by squaring, adding 1 or multiplying with 2
Last Updated :
06 Dec, 2021
Given a positive number N, the task is to find the number of ways to reach N from 2 wherein each operation one of the following can be performed:
- Add 1 to the current number.
- Multiply the current number by 2.
- Square the current number.
Example:
Input: N = 5
Output: 3
Explanation: The integer 5 can be reached by the following ways:
- 2 (+1) => 3 (+1) => 4 (+1) => 5
- 2 (*2) => 4 (+1) => 5
- 2 (^2) => 4 (+1) => 5
Therefore, the number of ways of reaching 5 from 2 are 3.
Input: N = 9
Output: 8
Approach: The given problem can be solved efficiently by using dynamic programming. The idea is to use a DP array to calculate the number of ways required to reach N from 2. Iterate the array Dp from 2 to N and after each iteration perform the mentioned operations to calculate the number of ways required to reach N i.e, Dp[i] => Dp[i+1], Dp[i] => Dp[2 * i], and Dp[i] => Dp[i * i]. Therefore, add the number of ways to reach Dp[i] in all the three states mentioned above. The value stored at Dp[N] is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int waysToReach( int N)
{
vector< int > Dp(N + 1, 0);
Dp[2] = 1;
for ( int i = 2; i <= N; i++) {
if (i + 1 <= N) {
Dp[i + 1] += Dp[i];
}
if (i * 2 <= N) {
Dp[i * 2] += Dp[i];
}
if (i * i <= N) {
Dp[i * i] += Dp[i];
}
}
return Dp[N];
}
int main()
{
int N = 5;
cout << waysToReach(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int waysToReach( int N)
{
int [] Dp = new int [N + 1 ];
Dp[ 2 ] = 1 ;
for ( int i = 2 ; i <= N; i++) {
if (i + 1 <= N) {
Dp[i + 1 ] += Dp[i];
}
if (i * 2 <= N) {
Dp[i * 2 ] += Dp[i];
}
if (i * i <= N) {
Dp[i * i] += Dp[i];
}
}
return Dp[N];
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(waysToReach(N));
}
}
|
Python3
def waysToReach(N):
Dp = [ 0 ] * (N + 1 )
Dp[ 2 ] = 1
for i in range ( 2 , N + 1 ):
if (i + 1 < = N):
Dp[i + 1 ] + = Dp[i]
if (i * 2 < = N):
Dp[i * 2 ] + = Dp[i]
if (i * i < = N):
Dp[i * i] + = Dp[i]
return Dp[N]
N = 5
print (waysToReach(N))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int waysToReach( int N)
{
int []Dp = new int [N+1];
for ( int i = 0; i < N+1; i++) {
Dp[i] = 0;
}
Dp[2] = 1;
for ( int i = 2; i <= N; i++) {
if (i + 1 <= N) {
Dp[i + 1] += Dp[i];
}
if (i * 2 <= N) {
Dp[i * 2] += Dp[i];
}
if (i * i <= N) {
Dp[i * i] += Dp[i];
}
}
return Dp[N];
}
public static void Main()
{
int N = 5;
Console.Write(waysToReach(N));
}
}
|
Javascript
<script>
function waysToReach(N) {
let Dp = new Array(N + 1).fill(0);
Dp[2] = 1;
for (let i = 2; i <= N; i++) {
if (i + 1 <= N) {
Dp[i + 1] += Dp[i];
}
if (i * 2 <= N) {
Dp[i * 2] += Dp[i];
}
if (i * i <= N) {
Dp[i * i] += Dp[i];
}
}
return Dp[N];
}
let N = 5;
document.write(waysToReach(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...