Reduce N to 1 in minimum moves by either multiplying by 2 or dividing by 6
Given an integer N, find the minimum number of operations to reduce N to 1 by using the following two operations:
- Multiply N by 2
- Divide N by 6, if N is divisible by 6
If N cannot be reduced to 1, print -1.
Examples:
Input: N = 54
Output: 5
Explanation: Perform the following operations–
- Divide N by 6 and get n = 9
- Multiply N by 2 and get n = 18
- Divide N by 6 and get n = 3
- Multiply N by 2 and get n = 6
- Divide N by 6 to get n = 1
Hence, minimum 5 operations needs to be performed to reduce 54 to 1
Input: N = 12
Output: -1
Approach: The task can be solved using following observations.
- If the number consists of primes other than 2 and 3 then the answer is -1 since N cannot be reduced to 1 with the above operations.
- Otherwise, let count2 be the number of two’s in the factorization of n and count3 be the number of three’s in the factorization of n.
- If count2 > count3 then the answer is -1 because we can’t get rid of all twos.
- Otherwise, the answer is (count3−count2) + count3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperations( int N)
{
int count2 = 0, count3 = 0;
while (N % 2 == 0) {
N = N / 2;
count2++;
}
while (N % 3 == 0) {
N = N / 3;
count3++;
}
if (N == 1 && (count2 <= count3)) {
cout << (2 * count3) - count2;
}
else {
cout << -1;
}
}
int main()
{
int N = 54;
minOperations(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minOperations( int N)
{
int count2 = 0 , count3 = 0 ;
while (N % 2 == 0 ) {
N = N / 2 ;
count2++;
}
while (N % 3 == 0 ) {
N = N / 3 ;
count3++;
}
if (N == 1 && (count2 <= count3)) {
System.out.print(( 2 * count3) - count2);
}
else {
System.out.print(- 1 );
}
}
public static void main(String[] args)
{
int N = 54 ;
minOperations(N);
}
}
|
Python3
def minOperations(N):
count2 = 0
count3 = 0
while (N % 2 = = 0 ):
N = N / / 2
count2 + = 1
while (N % 3 = = 0 ):
N = N / / 3
count3 + = 1
if (N = = 1 and (count2 < = count3)):
print (( 2 * count3) - count2)
else :
print ( - 1 )
if __name__ = = "__main__" :
N = 54
minOperations(N)
|
C#
using System;
class GFG {
static void minOperations( int N)
{
int count2 = 0, count3 = 0;
while (N % 2 == 0) {
N = N / 2;
count2++;
}
while (N % 3 == 0) {
N = N / 3;
count3++;
}
if (N == 1 && (count2 <= count3)) {
Console.WriteLine((2 * count3) - count2);
}
else {
Console.WriteLine(-1);
}
}
public static void Main()
{
int N = 54;
minOperations(N);
}
}
|
Javascript
<script>
function minOperations(N)
{
let count2 = 0, count3 = 0;
while (N % 2 == 0)
{
N = Math.floor(N / 2);
count2++;
}
while (N % 3 == 0)
{
N = Math.floor(N / 3);
count3++;
}
if (N == 1 && (count2 <= count3))
{
document.write((2 * count3) - count2);
}
else
{
document.write(-1);
}
}
let N = 54;
minOperations(N);
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Last Updated :
20 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...