Open In App

Minimum number of given operations required to reduce a number to 2

Given a positive integer N, the task is to reduce N to 2 by performing the following operations minimum number of times:

If it is not possible, print -1.

Examples:



Input: N = 28
Output: 3
Explanation: Operation 1: Subtract 3 from 28. Therefore, N becomes 28 – 3 = 25.
Operation 2: Divide 25 by 5. Therefore, N becomes 25 / 5 = 5.
Operation 3: Subtract 3 from 5. Therefore, N becomes 5 – 3 = 2. 
Hence, the minimum number of operations required is 3.

Input: n=10
Output: 1
Explanation: Operation 1: Divide 10 by 5, so n becomes 10/5=2.
Hence, the minimum operations required is 1.



Naive Approach: The idea is to recursively compute the minimum number of steps required.  

Time Complexity: O(2n)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use dynamic programming. Follow these steps to solve this problem.

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum number
// of operations required to reduce n to 2
int findMinOperations(int n)
{
    // Initialize a dp array
    int i, dp[n + 1];
 
    for (i = 0; i < n + 1; i++) {
        dp[i] = 999999;
    }
 
    // Handle the base case
    dp[2] = 0;
 
    // Iterate in the range [2, n]
    for (i = 2; i < n + 1; i++) {
 
        // Check if i * 5 <= n
        if (i * 5 <= n) {
 
            dp[i * 5] = min(
dp[i * 5], dp[i] + 1);
        }
 
        // Check if i + 3 <= n
        if (i + 3 <= n) {
 
            dp[i + 3] = min(
dp[i + 3], dp[i] + 1);
        }
    }
 
    // Return the result
    return dp[n];
}
 
// Driver code
int main()
{
    // Given Input
    int n = 28;
 
    // Function Call
    int m = findMinOperations(n);
 
    // Print the result
    if (m != 9999)
        cout << m;
    else
        cout << -1;
 
    return 0;
}




// Java program for the above approach
public class GFG {
 
    // Function to find the minimum number
    // of operations required to reduce n to 2
    static int findMinOperations(int n)
    {
        // Initialize a dp array
        int i = 0;
        int dp[] = new int[n + 1];
 
        for (i = 0; i < n + 1; i++) {
            dp[i] = 999999;
        }
 
        // Handle the base case
        dp[2] = 0;
 
        // Iterate in the range [2, n]
        for (i = 2; i < n + 1; i++) {
 
            // Check if i * 5 <= n
            if (i * 5 <= n) {
 
                dp[i * 5] = Math.min(dp[i * 5], dp[i] + 1);
            }
 
            // Check if i + 3 <= n
            if (i + 3 <= n) {
 
                dp[i + 3] = Math.min(dp[i + 3], dp[i] + 1);
            }
        }
 
        // Return the result
        return dp[n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
       
      // Given Input
        int n = 28;
 
        // Function Call
        int m = findMinOperations(n);
 
        // Print the result
        if (m != 9999)
            System.out.println(m);
        else
            System.out.println(-1);
    }
}
 
// This code is contributed by abhinavjain194




# Python3 program for the above approach
 
# Function to find the minimum number
# of operations required to reduce n to 2
def findMinOperations(n):
     
    # Initialize a dp array
    dp = [0 for i in range(n + 1)]
 
    for i in range(n + 1):
        dp[i] = 999999
 
    # Handle the base case
    dp[2] = 0
 
    # Iterate in the range [2, n]
    for i in range(2, n + 1):
         
        # Check if i * 5 <= n
        if (i * 5 <= n):
            dp[i * 5] = min(dp[i * 5],
                            dp[i] + 1)
                             
        # Check if i + 3 <= n
        if (i + 3 <= n):
            dp[i + 3] = min(dp[i + 3],
                           dp[i] + 1)
 
    # Return the result
    return dp[n]
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    n = 28
 
    # Function Call
    m = findMinOperations(n)
 
    # Print the result
    if (m != 9999):
        print (m)
    else:
        print (-1)
 
# This code is contributed by mohit kumar 29




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the minimum number
// of operations required to reduce n to 2
static int findMinOperations(int n)
{
     
    // Initialize a dp array
    int i;
    int []dp = new int[n + 1];
 
    for(i = 0; i < n + 1; i++)
    {
        dp[i] = 999999;
    }
 
    // Handle the base case
    dp[2] = 0;
 
    // Iterate in the range [2, n]
    for(i = 2; i < n + 1; i++)
    {
         
        // Check if i * 5 <= n
        if (i * 5 <= n)
        {
             
            dp[i * 5] = Math.Min(dp[i * 5],
                                dp[i] + 1);
        }
 
        // Check if i + 3 <= n
        if (i + 3 <= n)
        {
             
            dp[i + 3] = Math.Min(dp[i + 3],
                                dp[i] + 1);
        }
    }
 
    // Return the result
    return dp[n];
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int n = 28;
 
    // Function Call
    int m = findMinOperations(n);
 
    // Print the result
    if (m != 9999)
        Console.Write(m);
    else
        Console.Write(-1);
}
}
     
// This code is contributed by SURENDRA_GANGWAR




<script>
 
// JavaScript program for the above approach
 
// Function to find the minimum number
    // of operations required to reduce n to 2
function findMinOperations(n)
{
    // Initialize a dp array
        let i = 0;
        let dp = new Array(n + 1);
  
        for (i = 0; i < n + 1; i++) {
            dp[i] = 999999;
        }
  
        // Handle the base case
        dp[2] = 0;
  
        // Iterate in the range [2, n]
        for (i = 2; i < n + 1; i++) {
  
            // Check if i * 5 <= n
            if (i * 5 <= n) {
  
                dp[i * 5] = Math.min(dp[i * 5], dp[i] + 1);
            }
  
            // Check if i + 3 <= n
            if (i + 3 <= n) {
  
                dp[i + 3] = Math.min(dp[i + 3], dp[i] + 1);
            }
        }
  
        // Return the result
        return dp[n];
}
 
// Driver code
// Given Input
let n = 28;
 
// Function Call
let m = findMinOperations(n);
 
// Print the result
if (m != 9999)
    document.write(m);
else
    document.write(-1);
 
 
// This code is contributed by unknown2108
 
</script>

Output: 
3

 

Time Complexity: O(n)
Auxiliary Space: O(n)

 


Article Tags :