Minimum absolute difference between N and a power of 2
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 = 2floor(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++
#include <bits/stdc++.h>
using namespace std;
int minAbsDiff( int n)
{
int left = pow (2, floor (log2(n)));
int right = left * 2;
return min((n - left), (right - n));
}
int main()
{
int n = 15;
cout << minAbsDiff(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minAbsDiff( int n)
{
int left = ( int )Math.pow( 2 , ( int )(Math.log(n) /
Math.log( 2 )));
int right = left * 2 ;
return Math.min((n - left), (right - n));
}
public static void main(String args[])
{
int n = 15 ;
System.out.println(minAbsDiff(n));
}
}
|
Python3
from math import floor, log2
def minAbsDiff(n) :
left = pow ( 2 , floor(log2(n)))
right = left * 2
return min ((n - left), (right - n))
if __name__ = = "__main__" :
n = 15
print (minAbsDiff(n))
|
C#
using System;
class GFG
{
static double minAbsDiff( double n)
{
double left = Math.Pow(2,
Math.Floor(Math.Log(n, 2)));
double right = left * 2;
return Math.Min((n - left), (right - n));
}
public static void Main()
{
double n = 15;
Console.Write(minAbsDiff(n));
}
}
|
PHP
<?php
function minAbsDiff( $n )
{
$left = pow(2, floor (log( $n , 2)));
$right = $left * 2;
return min(( $n - $left ), ( $right - $n ));
}
$n = 15;
echo minAbsDiff( $n );
|
Javascript
<script>
function minAbsDiff(n)
{
let left = Math.pow(2, Math.floor(Math.log2(n, 2)));
let right = left * 2;
return Math.min((n - left), (right - n));
}
let n = 15;
document.write(minAbsDiff(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
We can use left shift operator to optimize the implementation.
C++
#include <bits/stdc++.h>
using namespace std;
int minAbsDiff( int n)
{
int left = 1 << (( int ) floor (log2(n)));
int right = left * 2;
return min((n - left), (right - n));
}
int main()
{
int n = 15;
cout << minAbsDiff(n);
return 0;
}
|
Java
class GFG
{
static int minAbsDiff( int n)
{
int left = 1 << (( int )Math.floor(Math.log(n) / Math.log( 2 )));
int right = left * 2 ;
return Math.min((n - left), (right - n));
}
public static void main (String[] args)
{
int n = 15 ;
System.out.println(minAbsDiff(n));
}
}
|
Python3
import math
def minAbsDiff(n):
left = 1 << ( int )(math.floor(math.log2(n)))
right = left * 2
return min ((n - left), (right - n))
if __name__ = = "__main__" :
n = 15
print (minAbsDiff(n))
|
C#
using System;
public class GFG
{
static int minAbsDiff( int n)
{
int left = 1 << (( int )Math.Floor(Math.Log(n) / Math.Log(2)));
int right = left * 2;
return Math.Min((n - left), (right - n));
}
static public void Main ()
{
int n = 15;
Console.WriteLine(minAbsDiff(n));
}
}
|
PHP
<?php
function minAbsDiff( $n )
{
$left = 1 << (( floor (log( $n ) / log(2))));
$right = $left * 2;
return min(( $n - $left ), ( $right - $n ));
}
$n = 15;
echo minAbsDiff( $n );
?>
|
Javascript
<script>
function minAbsDiff(n)
{
let left = 1 << (Math.floor(Math.log(n) / Math.log(2)));
let right = left * 2;
return Math.min((n - left), (right - n));
}
let n = 15;
document.write(minAbsDiff(n));
</script>
|
Time complexity : O(1)
Auxiliary Space: O(1)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...