# Greatest odd factor of an even number

• Last Updated : 05 Aug, 2021

Given an even number N, the task is to find the greatest possible odd factor of N.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 8642
Output: 4321
Explanation:
Here, factors of 8642 are {1, 8642, 2, 4321, 29, 298, 58, 149} in which odd factors are {1, 4321, 29, 149} and the greatest odd factor among all odd factors is 4321.

Input: N = 100
Output: 25
Explanation:
Here, factors of 100 are {1, 100, 2, 50, 4, 25, 5, 20, 10} in which odd factors are {1, 25, 5} and the greatest odd factor among all odd factors is 25.

Naive Approach: The naive approach is to find all the factors of N and then select the greatest odd factor from it.
Time Complexity: O(sqrt(N))

Efficient Approach: The efficient approach for this problem is to observe that every even number N can be represented as:

`N = 2i*odd_number`

Therefore to get the largest odd number we need to divide the given number N by 2 until N becomes an odd number.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include``using` `namespace` `std;` `// Function to print greatest odd factor``int` `greatestOddFactor(``int` `n)``{``    ``int` `pow_2 = (``int``)(``log``(n));``    ` `    ``// Initialize i with 1``    ``int` `i = 1;``    ` `    ``// Iterate till i <= pow_2``    ``while` `(i <= pow_2)``    ``{``        ` `        ``// Find the pow(2, i)``        ``int` `fac_2 = (2 * i);``        ``if` `(n % fac_2 == 0)``        ``{``            ``// If factor is odd, then``            ``// print the number and break``            ``if` `((n / fac_2) % 2 == 1)``            ``{``                ``return` `(n / fac_2);``            ``}``        ``}` `        ``i += 1;``    ``}``}` `// Driver Code``int` `main()``{``    ` `    ``// Given Number``    ``int` `N = 8642;``    ` `    ``// Function Call``    ``cout << greatestOddFactor(N);``    ``return` `0;``}` `// This code is contributed by Amit Katiyar`

## Java

 `// Java program for the above approach``class` `GFG{``    ` `// Function to print greatest odd factor``public` `static` `int` `greatestOddFactor(``int` `n)``{``    ``int` `pow_2 = (``int``)(Math.log(n));``    ` `    ``// Initialize i with 1``    ``int` `i = ``1``;``    ` `    ``// Iterate till i <= pow_2``    ``while` `(i <= pow_2)``    ``{``        ` `        ``// Find the pow(2, i)``        ``int` `fac_2 = (``2` `* i);``        ``if` `(n % fac_2 == ``0``)``        ``{``            ` `            ``// If factor is odd, then``            ``// print the number and break``            ``if` `((n / fac_2) % ``2` `== ``1``)``            ``{``                ``return` `(n / fac_2);``            ``}``        ``}``        ``i += ``1``;``    ``}``    ``return` `0``;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given Number``    ``int` `N = ``8642``;``    ` `    ``// Function Call``    ``System.out.println(greatestOddFactor(N));``}``}` `// This code is contributed by divyeshrabadiya07`

## Python3

 `# Python3 program for the above approach` `# importing Maths library``import` `math` `# Function to print greatest odd factor``def` `greatestOddFactor(n):``  ` `  ``pow_2 ``=` `int``(math.log(n, ``2``))``  ` `# Initialize i with 1``  ``i ``=` `1` `# Iterate till i <= pow_2``  ``while` `i <``=` `pow_2:` `# find the pow(2, i)``    ``fac_2 ``=` `(``2``*``*``i)` `    ``if` `(n ``%` `fac_2 ``=``=` `0``) :` `      ``# If factor is odd, then print the``      ``# number and break``      ``if` `( (n ``/``/` `fac_2) ``%` `2` `=``=` `1``):``        ``print``(n ``/``/` `fac_2)``        ``break` `    ``i ``+``=` `1` `# Driver Code` `# Given Number``N ``=` `8642` `# Function Call``greatestOddFactor(N)`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to print greatest odd factor``public` `static` `int` `greatestOddFactor(``int` `n)``{``    ``int` `pow_2 = (``int``)(Math.Log(n));``    ` `    ``// Initialize i with 1``    ``int` `i = 1;``    ` `    ``// Iterate till i <= pow_2``    ``while` `(i <= pow_2)``    ``{``        ` `        ``// Find the pow(2, i)``        ``int` `fac_2 = (2 * i);``        ``if` `(n % fac_2 == 0)``        ``{``            ` `            ``// If factor is odd, then``            ``// print the number and break``            ``if` `((n / fac_2) % 2 == 1)``            ``{``                ``return` `(n / fac_2);``            ``}``        ``}``        ``i += 1;``    ``}``    ``return` `0;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given number``    ``int` `N = 8642;``    ` `    ``// Function call``    ``Console.WriteLine(greatestOddFactor(N));``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``
Output:
`4321`

Time Complexity: O(log2(N))

My Personal Notes arrow_drop_up