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++
// C++ code to implement the approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Map to store minimum operations for any value of N
unordered_map<int, int> dp;
// Function to count steps required to reduce N to 1
int ReduceTo1(int N)
{
// If N reduced to 1
if (N <= 1)
return 0;
// If steps for N already found
if (dp.find(N) != dp.end())
return dp[N];
// Check the minimum steps from all given
// conditions
int min_op = 1
+ min(N % 2 + ReduceTo1(N / 2),
N % 3 + ReduceTo1(N / 3));
// Minimum steps required
dp[N] = min_op;
return min_op;
}
// Driver code
int main()
{
// Input value of N
int N = 10;
// Function call
cout << ReduceTo1(N);
return 0;
}
Java
import java.util.HashMap;
import java.util.Map;
public class ReduceTo1 {
// Map to store minimum operations for any value of N
static Map<Integer, Integer> dp = new HashMap<>();
// Function to count steps required to reduce N to 1
static int reduceTo1(int N) {
// If N reduced to 1
if (N <= 1)
return 0;
// If steps for N already found
if (dp.containsKey(N))
return dp.get(N);
// Check the minimum steps from all given conditions
int min_op = 1 +
Math.min(N % 2 + reduceTo1(N / 2),
N % 3 + reduceTo1(N / 3));
// Minimum steps required
dp.put(N, min_op);
return min_op;
}
// Driver code
public static void main(String[] args) {
// Input value of N
int N = 10;
// Function call
System.out.println(reduceTo1(N));
}
}
Python3
# Python Implementation
# Dictionary to store minimum operations for any value of N
dp = {}
# Function to count steps required to reduce N to 1
def ReduceTo1(N):
# If N is already 1, no steps needed
if N <= 1:
return 0
# If steps for N already found
if N in dp:
return dp[N]
# Check the minimum steps from all given conditions
min_op = 1 + min(N % 2 + ReduceTo1(N // 2), N % 3 + ReduceTo1(N // 3))
# Minimum steps required
dp[N] = min_op
return min_op
# Driver code
if __name__ == "__main__":
# Input value of N
N = 10
# Function call
print(ReduceTo1(N))
# This code is contributed by Sakshi
C#
using System;
using System.Collections.Generic;
public class ReduceTo1Example
{
// Dictionary to store minimum operations for any value of N
static Dictionary<int, int> dp = new Dictionary<int, int>();
// Function to count steps required to reduce N to 1
static int ReduceTo1(int N)
{
// If N reduced to 1
if (N <= 1)
return 0;
// If steps for N already found
if (dp.ContainsKey(N))
return dp[N];
// Check the minimum steps from all given conditions
int min_op = 1
+ Math.Min(N % 2 + ReduceTo1(N / 2),
N % 3 + ReduceTo1(N / 3));
// Minimum steps required
dp[N] = min_op;
return min_op;
}
// Driver code
public static void Main()
{
// Input value of N
int N = 10;
// Function call
Console.WriteLine(ReduceTo1(N));
}
}
JavaScript
// JavaScript code to implement the approach
// Map to store minimum operations
// for any value of N
const dp = new Map();
// Function to count steps required
// to reduce N to 1
function reduceTo1(N) {
// If N reduced to 1
if (N <= 1) return 0;
// If steps for N already found
if (dp.has(N)) return dp.get(N);
// Check the minimum steps from
// all given conditions
const min_op =
1 + Math.min(N % 2 + reduceTo1(Math.floor(N / 2)), N % 3 + reduceTo1(Math.floor(N / 3)));
// Minimum steps required
dp.set(N, min_op);
return min_op;
}
// Driver code
// Input value of N
const N = 10;
// Function call
console.log(reduceTo1(N));
Time Complexity: O(Log N)
Auxiliary Space: O(Log N)
Similar Reads
Reduce a number to 1 by performing given operations Given a number N. The task is to reduce the given number N to 1 in the minimum number of steps. You can perform any one of the below operations in each step.Operation 1: If the number is even then you can divide the number by 2.Operation 2: If the number is odd then you are allowed to perform either
7 min read
Reduce N to 0 or less by given X and Y operations Given three integers N, X, and Y, the task is to check if it is possible to reduce N to 0 or less by the following operations: Update N to ?N/2? + 10, at most X timesUpdate N to N - 10, at most Y times. Example: Input: N = 100, X = 3, Y = 4Output: YesExplanation:Update N = 100 to ?100/2? + 10 = 60.U
6 min read
Minimum steps to reduce N to 0 by given operations Give an integer N, the task is to find the minimum number of moves to reduce N to 0 by one of the following operations: Reduce N by 1.Reduce N to (N/2), if N is divisible by 2.Reduce N to (N/3), if N is divisible by 3. Examples: Input: N = 10Output: 4Explanation: Here N = 10Step 1: Reducing N by 1 i
11 min read
Reduce a number to 1 by performing given operations | Set 2 Given an integer N. The task is to reduce the given number N to 1 in minimum number of given operations. You can perform any one of the below operations in each step. If the number is even then you can divide the number by 2.If the number is odd then you are allowed to perform either (N + 1) or (N -
7 min read
Reduce a number to 1 by performing given operations | Set 3 Given an integer N, the task is to find the number of steps required to reduce the given number N to 1 by performing the following operations: If the number is a power of 2, then divide the number by 2.Otherwise, subtract the greatest power of 2 smaller than N from N. Examples: Input: N = 2 Output:
10 min read