Open in App
Not now

# Nearest smaller power of 2 for every digit of a number

• Last Updated : 03 May, 2021

Given an integer num, the task for every digit of the number is to find the highest power of 2 not exceeding that digit.

Examples:

Input: num = 4317
Output: 4214
Explanation:
The highest power of 2 â‰¤ 4 is 4.
The highest power of 2 â‰¤ 3 is 2.
The highest power of 2 â‰¤ 1 is 1.
The highest power of 2 â‰¤ 7 is 4.

Input: num = 8015
Output: 8014

Approach: Follow the steps below to solve the problem:

1. Convert the number to its equivalent string.
2. Traverse the string.
3. If the digit is ‘0’, print 0.
4. Otherwise, for every digit x, calculate 2(log2(x)).

Below is the implementation of the above approach:

## C++

 `// C++ program to implement` `// the above approach` `#include ` `using` `namespace` `std;`   `// Function to find the nearest power of` `// two for every digit of a given number` `void` `highestPowerOfTwo(``int` `num)` `{` `    ``// Converting number to string` `    ``string s = to_string(num);`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < (``int``)s.size();` `         ``i++) {`   `        ``if` `(s[i] == ``'0'``) {` `            ``cout << ``"0"``;` `            ``continue``;` `        ``}`   `        ``// Calculate log base 2` `        ``// of the current digit s[i]` `        ``int` `lg = log2(``int``(s[i]) - 48);`   `        ``// Highest power of 2 <= s[i]` `        ``int` `p = ``pow``(2, lg);`   `        ``// ASCII conversion` `        ``cout << ``char``(p + 48);` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `num = 4317;` `    ``highestPowerOfTwo(num);`   `    ``return` `0;` `}`

## Java

 `// Java program to implement ` `// the above approach ` `import` `java.util.*;` `class` `GFG` `{`   `  ``// Function to find the nearest power of` `  ``// two for every digit of a given number` `  ``static` `void` `highestPowerOfTwo(``int` `num)` `  ``{`   `    ``// Converting number to string` `    ``String s = Integer.toString(num);`   `    ``// Traverse the array` `    ``for` `(``int` `i = ``0``; i < (``int``)s.length(); i++)` `    ``{`   `      ``if` `(s.charAt(i) == ``'0'``)` `      ``{` `        ``System.out.print(``"0"``);` `        ``continue``;` `      ``}`   `      ``// Calculate log base 2` `      ``// of the current digit s[i]` `      ``int` `lg` `        ``= (``int``)(Math.log(s.charAt(i) - ``'0'``) / Math.log(``2``));`   `      ``// Highest power of 2 <= s[i]` `      ``int` `p = (``int``)Math.pow(``2``, lg);`   `      ``// ASCII conversion` `      ``System.out.print((``char``)(p + ``48``));` `    ``}` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String args[])` `  ``{` `    ``int` `num = ``4317``;` `    ``highestPowerOfTwo(num);` `  ``}` `}`   `// This code is contributed by susmitakundugoaldanga.`

## Python3

 `# Python 3 program for the above approach` `import` `math`   `# Function to find the nearest power of` `# two for every digit of a given number` `def` `highestPowerOfTwo(num) :` `    `  `    ``# Converting number to string` `    ``s ``=` `str``(num)`   `    ``# Traverse the array` `    ``for` `i ``in` `range``(``len``(s)):` `        ``if` `(s[i] ``=``=` `'0'``) :` `            ``print``(``"0"``)` `            ``continue` `        `  `        ``# Calculate log base 2` `        ``# of the current digit s[i]` `        ``lg ``=` `int``(math.log2(``ord``(s[i]) ``-` `48``))`   `        ``# Highest power of 2 <= s[i]` `        ``p ``=` `pow``(``2``, lg)`   `        ``# ASCII conversion` `        ``print``(``chr``(p ``+` `48``), end ``=` `"")` `    `  `# Driver Code` `num ``=` `4317` `highestPowerOfTwo(num)`   `# This code is contributed by code_hunt.`

## C#

 `// C# program to implement` `// the above approach` `using` `System;` `class` `GFG ` `{`   `    ``// Function to find the nearest power of` `    ``// two for every digit of a given number` `    ``static` `void` `highestPowerOfTwo(``int` `num)` `    ``{` `        ``// Converting number to string` `        ``String s = num.ToString();`   `        ``// Traverse the array` `        ``for` `(``int` `i = 0; i < (``int``)s.Length; i++)` `        ``{`   `            ``if` `(s[i] == ``'0'``)` `            ``{` `                ``Console.Write(``"0"``);` `                ``continue``;` `            ``}`   `            ``// Calculate log base 2` `            ``// of the current digit s[i]` `            ``int` `lg` `                ``= (``int``)(Math.Log(s[i] - ``'0'``) / Math.Log(2));`   `            ``// Highest power of 2 <= s[i]` `            ``int` `p = (``int``)Math.Pow(2, lg);`   `            ``// ASCII conversion` `            ``Console.Write((``char``)(p + 48));` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `num = 4317;` `        ``highestPowerOfTwo(num);` `    ``}` `}`   `// This code is contributed by subhammahato348.`

## Javascript

 ``

Output:

`4214`

Time Complexity: O(logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles