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 XY is 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++
#include <bits/stdc++.h>
using namespace std;
int minStep( long N)
{
if (N == 1)
return 0;
else if (N % 2 == 1)
return 1;
else {
float X;
int count = 0;
X = sqrt (N);
if (X == round(X)) {
return 1;
}
else {
while (N % 2 == 0) {
N /= 2;
count++;
}
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
int main()
{
long N = 35;
cout << (minStep(N));
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int minStep( long N)
{
if (N == 1)
return 0;
else if (N % 2 == 1)
return 1;
else {
float X;
int count = 0;
X = sqrt (N);
if (X == round(X)) {
return 1;
}
else {
while (N % 2 == 0) {
N /= 2;
count++;
}
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
int main()
{
long N = 35;
printf ( "%d" ,minStep(N));
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int minStep( long N)
{
if (N == 1 )
return 0 ;
else if (N % 2 == 1 )
return 1 ;
else {
Double X;
int count = 0 ;
X = Math.sqrt(N);
if (X == Math.round(X)) {
return 1 ;
}
else {
while (N % 2 == 0 ) {
N /= 2 ;
count++;
}
if (count % 2 == 0 )
return 2 ;
else
return - 1 ;
}
}
}
public static void main(String[] args)
{
long N = 35 ;
System.out.println(minStep(N));
}
}
|
Python3
import math
def minStep(N):
if N = = 1 :
return 0
elif N % 2 = = 1 :
return 1
else :
X = 0.0
count = 0
X = math.sqrt(N)
if X = = round (X):
return 1
else :
while N % 2 = = 0 :
N / = 2
count + = 1
if count % 2 = = 0 :
return 2
else :
return - 1
if __name__ = = "__main__" :
N = 35
print (minStep(N))
|
C#
using System;
class GFG {
static int minStep( long N)
{
if (N == 1)
return 0;
else if (N % 2 == 1)
return 1;
else {
double X = 0;
int count = 0;
X = Math.Sqrt(N);
if (X == Math.Round(X)) {
return 1;
}
else {
while (N % 2 == 0) {
N /= 2;
count++;
}
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
public static void Main()
{
long N = 35;
Console.WriteLine(minStep(N));
}
}
|
Javascript
<script>
function minStep(N)
{
if (N == 1)
return 0;
else if (N % 2 == 1)
return 1;
else {
let X;
let count = 0;
X = Math.sqrt(N);
if (X == Math.round(X)) {
return 1;
}
else {
while (N % 2 == 0) {
N = Math.floor(N/2);
count++;
}
if (count % 2 == 0)
return 2;
else
return -1;
}
}
}
let N = 35;
document.write(minStep(N), "</br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 May, 2022
Like Article
Save Article