Reduce N to 1 by given operations
Last Updated :
18 Jan, 2024
Given an integer N. Then your task is to output a minimum number of operations to reduce N into 1. You can below operations to do the same:
- Subtract 1 from N
- Update N to N/2, if N is divisible by 2
- Update N to N/3, if N is divisible by 3
Examples:
Input: N = 10
Output: 3
Explanation: The operations are performed as:
- First Operation: Subtract 1 form 10. Updated value of N = 9.
- Second Operation: N = 9 is divisible by 3. Update N as 9/3 = 3
- Second Operation: N = 3 is divisible by 3. Update N as 3/3 = 1
Now N is reduced into 1 by following sequence of N’s value as {10 → 9 → 3 → 1}. Thus, minimum number of operations required are 3.
Input: 4
Output: 2
Explanation: 4 can be reduced into 1 with two ways {4 → 3 → 1} or {4 → 2 → 1}. Both requires minimum operations as 2.
Approach: Implement the idea below to solve the problem
In this problem we have 3 choices to perform operations, in solving choice-based problems we can use Dynamic Programming. For this problem we will take help of Dynamic Programming in the form of map and for each value of N, we can explore all the given conditions and can proceed further with minimum of them.
Steps were taken to solve the problem:
- Initialize a map let say DP.
- Definition of recursive function ReduceTo1(N):
- If (N <= 1)
- If (map DP contains N)
- Create a variable let say Min_op to store minimum number of operations and initialize it equal to (1+min(N % 2 + ReduceTo1(N / 2), N % 3 + ReduceTo1(N / 3)))
- DP[N] = Min_op
- Return Min_op
Below is the code to implement the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
unordered_map< int , int > dp;
int ReduceTo1( int N)
{
if (N <= 1)
return 0;
if (dp.find(N) != dp.end())
return dp[N];
int min_op = 1
+ min(N % 2 + ReduceTo1(N / 2),
N % 3 + ReduceTo1(N / 3));
dp[N] = min_op;
return min_op;
}
int main()
{
int N = 10;
cout << ReduceTo1(N);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class ReduceTo1 {
static Map<Integer, Integer> dp = new HashMap<>();
static int reduceTo1( int N) {
if (N <= 1 )
return 0 ;
if (dp.containsKey(N))
return dp.get(N);
int min_op = 1 +
Math.min(N % 2 + reduceTo1(N / 2 ),
N % 3 + reduceTo1(N / 3 ));
dp.put(N, min_op);
return min_op;
}
public static void main(String[] args) {
int N = 10 ;
System.out.println(reduceTo1(N));
}
}
|
Python3
dp = {}
def ReduceTo1(N):
if N < = 1 :
return 0
if N in dp:
return dp[N]
min_op = 1 + min (N % 2 + ReduceTo1(N / / 2 ), N % 3 + ReduceTo1(N / / 3 ))
dp[N] = min_op
return min_op
if __name__ = = "__main__" :
N = 10
print (ReduceTo1(N))
|
C#
using System;
using System.Collections.Generic;
public class ReduceTo1Example
{
static Dictionary< int , int > dp = new Dictionary< int , int >();
static int ReduceTo1( int N)
{
if (N <= 1)
return 0;
if (dp.ContainsKey(N))
return dp[N];
int min_op = 1
+ Math.Min(N % 2 + ReduceTo1(N / 2),
N % 3 + ReduceTo1(N / 3));
dp[N] = min_op;
return min_op;
}
public static void Main()
{
int N = 10;
Console.WriteLine(ReduceTo1(N));
}
}
|
Javascript
const dp = new Map();
function reduceTo1(N) {
if (N <= 1) return 0;
if (dp.has(N)) return dp.get(N);
const min_op =
1 + Math.min(N % 2 + reduceTo1(Math.floor(N / 2)), N % 3 + reduceTo1(Math.floor(N / 3)));
dp.set(N, min_op);
return min_op;
}
const N = 10;
console.log(reduceTo1(N));
|
Time Complexity: O(Log N)
Auxiliary Space: O(Log N)
Share your thoughts in the comments
Please Login to comment...