Minimum absolute difference between N and any power of 2
Given a positive integer N, the task is to find the minimum absolute difference between N and any power of 2.
Examples:
Input: N = 3
Output: 1
Smaller power of 2 nearest to 3 is 2, abs(3 – 2) = 1
Higher power of 2 nearest to 3 is 4, abs(4 – 3) = 1
Input: N = 6
Output: 2
Approach:
- Find the highest power of 2 less than or equal to N and store it in a variable low.
- Find the smallest power of 2 greater than or equal to N and store it in a variable high.
- Now, the answer will be max(N – low, high – N).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the highest power // of 2 less than or equal to n int prevPowerof2(int n) { int p = (int)log2(n); return (int)pow(2, p); } // Function to return the smallest power // of 2 greater than or equal to n int nextPowerOf2(int n) { int p = 1; if (n && !(n & (n - 1))) return n; while (p < n) p <<= 1; return p; } // Function that returns the minimum // absolute difference between n // and any power of 2 int minDiff(int n) { int low = prevPowerof2(n); int high = nextPowerOf2(n); return min(n - low, high - n); } // Driver code int main() { int n = 6; cout << minDiff(n); return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the highest power // of 2 less than or equal to n static int prevPowerof2(int n) { int p = (int)(Math.log(n) / Math.log(2)); return (int)Math.pow(2, p); } // Function to return the smallest power // of 2 greater than or equal to n static int nextPowerOf2(int n) { int p = 1; if ((n == 0) && !((n & (n - 1)) == 0)) return n; while (p < n) p <<= 1; return p; } // Function that returns the minimum // absolute difference between n // and any power of 2 static int minDiff(int n) { int low = prevPowerof2(n); int high = nextPowerOf2(n); return Math.min(n - low, high - n); } // Driver code public static void main (String[] args) { int n = 6; System.out.println(minDiff(n)); } } // This code is contributed by AnkitRai01 |
chevron_right
filter_none
Python3
# Python3 implementation of the approach from math import log # Function to return the highest power # of 2 less than or equal to n def prevPowerof2(n): p = int(log(n)) return pow(2, p) # Function to return the smallest power # of 2 greater than or equal to n def nextPowerOf2(n): p = 1 if (n and (n & (n - 1)) == 0): return n while (p < n): p <<= 1 return p # Function that returns the minimum # absolute difference between n # and any power of 2 def minDiff(n): low = prevPowerof2(n) high = nextPowerOf2(n) return min(n - low, high - n) # Driver code n = 6 print(minDiff(n)) # This code is contributed by Mohit Kumar |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { // Function to return the highest power // of 2 less than or equal to n static int prevPowerof2(int n) { int p = (int)(Math.Log(n) / Math.Log(2)); return (int)Math.Pow(2, p); } // Function to return the smallest power // of 2 greater than or equal to n static int nextPowerOf2(int n) { int p = 1; if ((n == 0) && !((n & (n - 1)) == 0)) return n; while (p < n) p <<= 1; return p; } // Function that returns the minimum // absolute difference between n // and any power of 2 static int minDiff(int n) { int low = prevPowerof2(n); int high = nextPowerOf2(n); return Math.Min(n - low, high - n); } // Driver code public static void Main (String []args) { int n = 6; Console.WriteLine(minDiff(n)); } } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
Output:
2
Recommended Posts:
- Program to find whether a no is power of two
- Power Set
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Write you own Power without using multiplication(*) and division(/) operators
- Check if a number can be expressed as x^y (x raised to power y)
- Find minimum number of coins that make a given value
- Minimum number of squares whose sum equals to given number n
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Modular Exponentiation (Power in Modular Arithmetic)
- Count minimum steps to get the given desired array
- Check if a number is a power of another number
- Find minimum number of merge operations to make an array palindrome
- Find the minimum difference between Shifted tables of two numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



