Given an integer **N**, the task is to find the minimum absolute difference between **N** and a power of **2**.

**Examples:**

Input:N = 4

Output:0

Power of 2 closest to 4 is 4. Therefore the minimum difference possible is 0.

Input:N = 9

Output:1

Power of 2 closest to 9 is 8 and 9 – 8 = 1

**Approach:** Find the power of **2** closest to **N** on its left, **left = 2 ^{floor(log2(N))}** then the closest power of

**2**on

**N’s**right will be

**left * 2**. Now the minimum absolute difference will be the minimum of

**N – left**and

**right – N**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum difference ` `// between N and a power of 2 ` `int` `minAbsDiff(` `int` `n) ` `{ ` ` ` `// Power of 2 closest to n on its left ` ` ` `int` `left = ` `pow` `(2, ` `floor` `(log2(n))); ` ` ` ` ` `// Power of 2 closest to n on its right ` ` ` `int` `right = left * 2; ` ` ` ` ` `// Return the minimum abs difference ` ` ` `return` `min((n - left), (right - n)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` `cout << minAbsDiff(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` ` ` `# from math lib import floor ` `# and log2 function ` `from` `math ` `import` `floor, log2 ` ` ` `# Function to return the minimum ` `# difference between N and a power of 2 ` `def` `minAbsDiff(n) : ` ` ` ` ` `# Power of 2 closest to n on its left ` ` ` `left ` `=` `pow` `(` `2` `, floor(log2(n))) ` ` ` ` ` `# Power of 2 closest to n on its right ` ` ` `right ` `=` `left ` `*` `2` ` ` ` ` `# Return the minimum abs difference ` ` ` `return` `min` `((n ` `-` `left), (right ` `-` `n)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `15` ` ` `print` `(minAbsDiff(n)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to return the minimum difference ` `// between N and a power of 2 ` `static` `double` `minAbsDiff(` `double` `n) ` `{ ` ` ` `// Power of 2 closest to n on its left ` ` ` `double` `left = Math.Pow(2, ` ` ` `Math.Floor(Math.Log(n, 2))); ` ` ` ` ` `// Power of 2 closest to n on its right ` ` ` `double` `right = left * 2; ` ` ` ` ` `// Return the minimum abs difference ` ` ` `return` `Math.Min((n - left), (right - n)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `double` `n = 15; ` ` ` `Console.Write(minAbsDiff(n)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to return the minimum difference ` `// between N and a power of 2 ` `function` `minAbsDiff(` `$n` `) ` `{ ` ` ` `// Power of 2 closest to n on its left ` ` ` `$left` `= pow(2, ` `floor` `(log(` `$n` `, 2))); ` ` ` ` ` `// Power of 2 closest to n on its right ` ` ` `$right` `= ` `$left` `* 2; ` ` ` ` ` `// Return the minimum abs difference ` ` ` `return` `min((` `$n` `- ` `$left` `), (` `$right` `- ` `$n` `)); ` `} ` ` ` `// Driver code ` `$n` `= 15; ` `echo` `minAbsDiff(` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

**Output:**

1

We can use left shift operator to optimize the implementation.

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum difference ` `// between N and a power of 2 ` `int` `minAbsDiff(` `int` `n) ` `{ ` ` ` `// Power of 2 closest to n on its left ` ` ` `int` `left = 1 << ((` `int` `)` `floor` `(log2(n))); ` ` ` ` ` `// Power of 2 closest to n on its right ` ` ` `int` `right = left * 2; ` ` ` ` ` `// Return the minimum abs difference ` ` ` `return` `min((n - left), (right - n)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` `cout << minAbsDiff(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` `import` `math ` ` ` `# Function to return the minimum ` `# difference between N and a power of 2 ` `def` `minAbsDiff(n): ` ` ` ` ` `# Power of 2 closest to n on its left ` ` ` `left ` `=` `1` `<< (` `int` `)(math.floor(math.log2(n))) ` ` ` ` ` `# Power of 2 closest to n on its right ` ` ` `right ` `=` `left ` `*` `2` ` ` ` ` `# Return the minimum abs difference ` ` ` `return` `min` `((n ` `-` `left), (right ` `-` `n)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `15` ` ` `print` `(minAbsDiff(n)) ` ` ` `# This code is contributed ` `# by 29AjayKumar ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to return the minimum difference ` `// between N and a power of 2 ` `function` `minAbsDiff(` `$n` `) ` `{ ` ` ` `// Power of 2 closest to n on its left ` ` ` `$left` `= 1 << ((` `floor` `(log(` `$n` `) / log(2)))); ` ` ` ` ` `// Power of 2 closest to n on its right ` ` ` `$right` `= ` `$left` `* 2; ` ` ` ` ` `// Return the minimum abs difference ` ` ` `return` `min((` `$n` `- ` `$left` `), (` `$right` `- ` `$n` `)); ` `} ` ` ` `// Driver code ` `$n` `= 15; ` `echo` `minAbsDiff(` `$n` `); ` ` ` `// This code is contributed by ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Largest subset where absolute difference of any two element is a power of 2
- Pair of prime numbers with a given sum and minimum absolute difference
- Minimum absolute difference of a number and its closest prime
- Pair with minimum absolute difference after solving each query
- Minimize the absolute difference of sum of two subsets
- Absolute Difference of even and odd indexed elements in an Array
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Absolute difference between sum and product of roots of a quartic equation
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Absolute Difference of all pairwise consecutive elements in an array
- Check if absolute difference of consecutive nodes is 1 in Linked List
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Minimum number of power terms with sum equal to n
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum splits in a binary string such that every substring is a power of 4 or 6.

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.