Minimize operations to reduce N to 2 by repeatedly reducing by 3 or dividing by 5
Last Updated :
18 Aug, 2021
Given a positive integer N, the task is to find the minimum number of operations needed to convert N to 2 either by decrementing N by 3 or dividing N by 5 if N is divisible by 5. If it is not possible to reduce N to 2, then print “-1”.
Examples:
Input: N =10
Output: 1
Explanation:
Following are the operations performed to reduce N to 2:
- Dividing N by 5, reduces N to 10/5 = 2.
After the above operations, N is reduced to 2. Therefore, the minimum number of operations required is 1.
Input: N = 25
Output: 2
Approach: The given problem can be solved by using Dynamic Programming, the idea is to start iterating from 2 and perform both the operations in a reverse manner i.e., instead of subtracting, perform addition of 3 and instead of dividing, perform multiplication with 5 at every state and store the minimum number of operations for every possible value of N in the array dp[].
If the value of N is reached, then print the value of dp[N] as the minimum number of operations. Otherwise, print -1. Follow the steps below to solve the problem:
- Initialize an auxiliary array, say dp[] of size (N + 1), and initialize all array elements with INT_MAX.
- Set the value of dp[2] equal to 0.
- Iterate over the range [0, N], and update the value of dp[i] as:
- dp[i * 5] = min(dp[i * 5], dp[i] + 1).
- dp[i + 3] = min(dp[i + 3], dp[i] + 1).
- If the value of dp[N] is INT_MAX, then print -1. Otherwise, print dp[N] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations( int N)
{
int dp[N + 1];
int i;
for ( int i = 0; i <= N; i++) {
dp[i] = 1e9;
}
dp[2] = 0;
for (i = 2; i <= N; i++) {
if (dp[i] == 1e9)
continue ;
if (i * 5 <= N) {
dp[i * 5] = min(dp[i * 5],
dp[i] + 1);
}
if (i + 3 <= N) {
dp[i + 3] = min(dp[i + 3],
dp[i] + 1);
}
}
if (dp[N] == 1e9)
return -1;
return dp[N];
}
int main()
{
int N = 25;
cout << minimumOperations(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minimumOperations( int N)
{
int [] dp = new int [N + 1 ];
int i;
for (i = 0 ; i <= N; i++) {
dp[i] = ( int )1e9;
}
dp[ 2 ] = 0 ;
for (i = 2 ; i <= N; i++) {
if (dp[i] == ( int )1e9)
continue ;
if (i * 5 <= N) {
dp[i * 5 ] = Math.min(dp[i * 5 ], dp[i] + 1 );
}
if (i + 3 <= N) {
dp[i + 3 ] = Math.min(dp[i + 3 ], dp[i] + 1 );
}
}
if (dp[N] == 1e9)
return - 1 ;
return dp[N];
}
public static void main(String[] args)
{
int N = 25 ;
System.out.println(minimumOperations(N));
}
}
|
C#
using System;
class GFG{
static int minimumOperations( int N)
{
int [] dp = new int [N + 1];
int i;
for (i = 0; i <= N; i++) {
dp[i] = ( int )1e9;
}
dp[2] = 0;
for (i = 2; i <= N; i++) {
if (dp[i] == ( int )1e9)
continue ;
if (i * 5 <= N) {
dp[i * 5] = Math.Min(dp[i * 5], dp[i] + 1);
}
if (i + 3 <= N) {
dp[i + 3] = Math.Min(dp[i + 3], dp[i] + 1);
}
}
if (dp[N] == 1e9)
return -1;
return dp[N];
}
public static void Main(String[] args)
{
int N = 25;
Console.Write(minimumOperations(N));
}
}
|
Javascript
<script>
function minimumOperations(N)
{
let dp = new Array(N + 1);
let i;
for (i = 0; i <= N; i++) {
dp[i] = 1e9;
}
dp[2] = 0;
for (i = 2; i <= N; i++) {
if (dp[i] == 1e9)
continue ;
if (i * 5 <= N) {
dp[i * 5] = Math.min(dp[i * 5], dp[i] + 1);
}
if (i + 3 <= N) {
dp[i + 3] = Math.min(dp[i + 3], dp[i] + 1);
}
}
if (dp[N] == 1e9)
return -1;
return dp[N];
}
let N = 25;
document.write(minimumOperations(N));
</script>
|
Python3
def minimumOperations(N):
dp = [ 0 for i in range (N + 1 )]
for i in range (N + 1 ):
dp[i] = 1000000000
dp[ 2 ] = 0
for i in range ( 2 ,N + 1 , 1 ):
if (dp[i] = = 1000000000 ):
continue
if (i * 5 < = N):
dp[i * 5 ] = min (dp[i * 5 ], dp[i] + 1 )
if (i + 3 < = N):
dp[i + 3 ] = min (dp[i + 3 ], dp[i] + 1 )
if (dp[N] = = 1000000000 ):
return - 1
return dp[N]
if __name__ = = '__main__' :
N = 25
print (minimumOperations(N))
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...