Given a positive integer N, the task is to find the minimum absolute difference between N and any power of 2.
Input: N = 3
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
- 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:
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.
- Largest subset where absolute difference of any two element is a power of 2
- Minimum absolute difference between N and a power of 2
- Check if any permutation of N equals any power of K
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Sum of absolute difference of all pairs raised to power K
- Find power of power under mod of a prime
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Pair of prime numbers with a given sum and minimum absolute difference
- Minimum absolute difference of a number and its closest prime
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Count pairs in Array whose product is a Kth power of any positive integer
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Absolute difference between the first X and last X Digits of N
- Absolute difference between sum and product of roots of a quartic equation
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.