Minimum positive integer to divide a number such that the result is an odd
Given a number n. Print the minimum positive integer by which it should be divided so that a result is an odd number.
Examples :
Input : 36
Output : 4
36 must be divided by 4 or 12 to make it odd.
We take minimum of 4 and 12 i.e. 4
Input : 8
Output : 8
8 must be divided by 8 to make it an odd number.
Method 1:
Find the minimum number that makes the given number
odd by dividing it one by one from 2(i) to N
If (N/i) is odd then return i.
C++
#include <iostream>
using namespace std;
int makeOdd( int n)
{
if (n % 2 != 0)
return 1;
for ( int i = 2 ; i <= n ; i++)
if ((n % i == 0) && ((n / i) % 2 == 1))
return i;
}
int main()
{
int n = 36;
cout << makeOdd(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int makeOdd( int n)
{
if (n % 2 != 0 )
return 1 ;
int i;
for (i = 2 ; i <= n ; i++)
if ((n % i == 0 ) && ((n / i) % 2 == 1 ))
break ;
return i;
}
public static void main (String[] args)
{
int n = 36 ;
int res = makeOdd(n);
System.out.println(res);
}
}
|
Python3
def makeOdd(n):
if n % 2 ! = 0 :
return 1 ;
for i in range ( 2 , n):
if (n % i = = 0 and
( int )(n / i) % 2 = = 1 ):
return i;
n = 36 ;
print (makeOdd(n));
|
C#
using System;
class GFG
{
static int makeOdd( int n)
{
if (n % 2 != 0)
return 1;
int i;
for (i = 2 ; i <= n ; i++)
if ((n % i == 0) && ((n / i) % 2 == 1))
break ;
return i;
}
public static void Main ()
{
int n = 36;
int res = makeOdd(n);
Console.Write(res);
}
}
|
PHP
<?php
function makeOdd( $n )
{
if ( $n % 2 != 0)
return 1;
for ( $i = 2 ; $i <= $n ; $i ++)
if (( $n % $i == 0) &&
(( $n / $i ) % 2 == 1))
return $i ;
}
$n = 36;
echo makeOdd( $n );
?>
|
Javascript
<script>
function makeOdd(n)
{
if (n % 2 != 0)
return 1;
var i;
for (i = 2 ; i <= n ; i++)
if ((n % i == 0) && ((n / i) % 2 == 1))
break ;
return i;
}
var n = 36;
var res = makeOdd(n);
document.write(res);
</script>
|
Time complexity: O(n) where n is a given number
Auxiliary space: O(1)
Method 2:
As there is only one case to make a number odd i.e.
It is an even number i.e. divisible by 2.
So, the point is to find the smallest multiple of 2
that can make the given number odd.
C++
#include <iostream>
using namespace std;
int makeOdd( int n)
{
if (n%2 != 0)
return 1;
int result = 1;
while (n%2 == 0)
{
n /= 2;
result *= 2;
}
return result;
}
int main()
{
int n = 36;
cout << makeOdd(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int makeOdd( int n)
{
if (n % 2 != 0 )
return 1 ;
int ans = 1 ;
while (n % 2 == 0 )
{
n /= 2 ;
ans *= 2 ;
}
return ans;
}
public static void main (String[] args)
{
int n = 36 ;
int res = makeOdd(n);
System.out.println(res);
}
}
|
Python3
def makeOdd(n):
if (n % 2 ! = 0 ):
return 1 ;
result = 1 ;
while (n % 2 = = 0 ):
n = n / 2 ;
result = result * 2 ;
return result;
n = 36 ;
print (makeOdd(n));
|
C#
using System;
class GFG {
static int makeOdd( int n)
{
if (n % 2 != 0)
return 1;
int ans = 1;
while (n % 2 == 0)
{
n /= 2;
ans *= 2;
}
return ans;
}
public static void Main ()
{
int n = 36;
int res = makeOdd(n);
Console.Write(res);
}
}
|
PHP
<?php
function makeOdd( $n )
{
if ( $n % 2 != 0)
return 1;
$result = 1;
while ( $n % 2 == 0)
{
$n /= 2;
$resul *= 2;
}
return $resul ;
}
$n = 36;
echo makeOdd( $n );
?>
|
Javascript
<script>
function makeOdd( n) {
if (n % 2 != 0)
return 1;
var ans = 1;
while (n % 2 == 0) {
n = parseInt(n/2);
ans *= 2;
}
return ans;
}
var n = 36;
var res = makeOdd(n);
document.write(res);
</script>
|
Time complexity: O(log2n) where n is a given number
Auxiliary space: O(1)
Last Updated :
14 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...