Given an integer N, the task is to find the minimum number of divisions required to reduce the number to 1 when the divisions follow the given criteria:
- Pick two integers X and Y such that X+Y is even.
- Replace N with N/XY where XYis a divisor of N
Note: If it is impossible to reduce N to 1, then return -1.
Examples:
Input: N = 35
Output: 1
Explanation: Select X = 35 and Y = 1. X+Y = 36 which is even,
and XY = 35 is a divisor of N = 35 so this is a valid choice.Input: 44
Output: 2
Approach: The problem can be solved based on the following mathematical observation:
- If N is odd then X can be chosen as X = N and Y = 1. So XY = X = N and also X + Y = X + 1 = even.
- If N is even then N can be written as N = 2p * X where p is any integer and X is an odd number (can be 1).
- If p is odd then it is never possible to reduce the number to 1 as 2 + p will always be odd.
- Otherwise, it will take 2 moves to reduce the number to 1: One step to reduce the number to X and another step to reduce it from X to 1.
- If X = 1 then no 2nd step is needed and 1 step will be sufficient.
Follow the steps mentioned below to solve the problem:
- Find if the number is odd or even.
- If the number is even then:
- If 2 is raised to an odd power, then the answer is not possible.
- Otherwise, get the minimum number of steps as shown in the above observation.
- If the number is odd, it takes only one step.
Below is the implementation of the above approach.
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find
// the minimum number of moves
int minStep(long N)
{
if (N == 1)
return 0;
// If number input by user is odd
else if (N % 2 == 1)
return 1;
else {
float X;
int count = 0;
// Finding square root of
// integer input by user
X = sqrt(N);
// Is perfect square
if (X == round(X)) {
return 1;
}
else {
// Checking number is divisible by 2
while (N % 2 == 0) {
N /= 2;
count++;
}
// Check value of count is
// odd or even
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
// Driver code
int main()
{
long N = 35;
// Function call
cout << (minStep(N));
return 0;
}
// This code is contributed by rakeshsahni
// C code to implement the approach
#include <math.h>
#include <stdio.h>
// Function to find
// the minimum number of moves
int minStep(long N)
{
if (N == 1)
return 0;
// If number input by user is odd
else if (N % 2 == 1)
return 1;
else {
float X;
int count = 0;
// Finding square root of
// integer input by user
X = sqrt(N);
// Is perfect square
if (X == round(X)) {
return 1;
}
else {
// Checking number is divisible by 2
while (N % 2 == 0) {
N /= 2;
count++;
}
// Check value of count is
// odd or even
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
// Driver code
int main()
{
long N = 35;
// Function call
printf("%d",minStep(N));
return 0;
}
// This code is contributed by Aarohi Rai
// java code to implement the approach
import java.util.*;
class GFG {
// Function to find
// the minimum number of moves
public static int minStep(long N)
{
if (N == 1)
return 0;
// If number input by user is odd
else if (N % 2 == 1)
return 1;
else {
Double X;
int count = 0;
// Finding square root of
// integer input by user
X = Math.sqrt(N);
// Is perfect square
if (X == Math.round(X)) {
return 1;
}
else {
// Checking number is divisible by 2
while (N % 2 == 0) {
N /= 2;
count++;
}
// Check value of count is
// odd or even
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
// Driver code
public static void main(String[] args)
{
long N = 35;
// Function call
System.out.println(minStep(N));
}
}
# Python code to implement the approach
import math
# Function to find
# the minimum number of moves
def minStep(N):
if N == 1:
return 0
# If number input by user is odd
elif N % 2 == 1:
return 1
else:
X = 0.0
count = 0
# Finding square root of
# integer input by user
X = math.sqrt(N)
# Is perfect square
if X == round(X):
return 1
else:
# Checking number is divisible by 2
while N % 2 == 0:
N /= 2
count += 1
# Check value of count is
# odd or even
if count % 2 == 0:
return 2
else:
return -1
# Driver code
if __name__ == "__main__":
N = 35
# Function call
print(minStep(N))
# This code is contributed by Rohit Pradhan
// C# code to implement the approach
using System;
class GFG {
// Function to find
// the minimum number of moves
static int minStep(long N)
{
if (N == 1)
return 0;
// If number input by user is odd
else if (N % 2 == 1)
return 1;
else {
double X = 0;
int count = 0;
// Finding square root of
// integer input by user
X = Math.Sqrt(N);
// Is perfect square
if (X == Math.Round(X)) {
return 1;
}
else {
// Checking number is divisible by 2
while (N % 2 == 0) {
N /= 2;
count++;
}
// Check value of count is
// odd or even
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
// Driver code
public static void Main()
{
long N = 35;
// Function call
Console.WriteLine(minStep(N));
}
}
// This code is contributed by Samim Hossain Mondal.
<script>
// JavaScript code to implement the approach
// Function to find
// the minimum number of moves
function minStep(N)
{
if (N == 1)
return 0;
// If number input by user is odd
else if (N % 2 == 1)
return 1;
else {
let X;
let count = 0;
// Finding square root of
// integer input by user
X = Math.sqrt(N);
// Is perfect square
if (X == Math.round(X)) {
return 1;
}
else {
// Checking number is divisible by 2
while (N % 2 == 0) {
N = Math.floor(N/2);
count++;
}
// Check value of count is
// odd or even
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
// Driver code
let N = 35;
// Function call
document.write(minStep(N),"</br>");
// This code is contributed by shinjanpatra
</script>
Output
1
Time Complexity: O(1)
Auxiliary Space: O(1)