给定整数N ,任务是找到N与2的幂之间的最小绝对差。
例子:
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
方法:找到最接近N的2的幂在左边,左边= 2 floor(log2(N)),那么最右边N的2的幂将成为左* 2 。现在,最小绝对差将是N-左和右-N的最小值。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
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;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
// Function to return the minimum difference
// between N and a power of 2
static int minAbsDiff(int n)
{
// Power of 2 closest to n on its left
int left = (int)Math.pow(2, (int)(Math.log(n) /
Math.log(2)));
// Power of 2 closest to n on its right
int right = left * 2;
// Return the minimum abs difference
return Math.min((n - left), (right - n));
}
// Driver code
public static void main(String args[])
{
int n = 15;
System.out.println(minAbsDiff(n));
}
}
// This code is contributed by
// Surendra_Gangwar
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
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
PHP
C++
// C++ implementation of the above approach
#include
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;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the minimum difference
// between N and a power of 2
static int minAbsDiff(int n)
{
// Power of 2 closest to n on its left
int left = 1 << ((int)Math.floor(Math.log(n) / Math.log(2)));
// Power of 2 closest to n on its right
int right = left * 2;
// Return the minimum abs difference
return Math.min((n - left), (right - n));
}
// Driver code
public static void main (String[] args)
{
int n = 15;
System.out.println(minAbsDiff(n));
}
}
// This code is contributed by chandan_jnu
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
C#
// C# implementation of the above approach
using System;
public class GFG
{
// Function to return the minimum difference
// between N and a power of 2
static int minAbsDiff(int n)
{
// Power of 2 closest to n on its left
int left = 1 << ((int)Math.Floor(Math.Log(n) / Math.Log(2)));
// Power of 2 closest to n on its right
int right = left * 2;
// Return the minimum abs difference
return Math.Min((n - left), (right - n));
}
// Driver code
static public void Main ()
{
int n = 15;
Console.WriteLine(minAbsDiff(n));
}
}
// This code is contributed by jit_t.
PHP
输出:
1
我们可以使用左移运算符来优化实现。
C++
// C++ implementation of the above approach
#include
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;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the minimum difference
// between N and a power of 2
static int minAbsDiff(int n)
{
// Power of 2 closest to n on its left
int left = 1 << ((int)Math.floor(Math.log(n) / Math.log(2)));
// Power of 2 closest to n on its right
int right = left * 2;
// Return the minimum abs difference
return Math.min((n - left), (right - n));
}
// Driver code
public static void main (String[] args)
{
int n = 15;
System.out.println(minAbsDiff(n));
}
}
// This code is contributed by chandan_jnu
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
C#
// C# implementation of the above approach
using System;
public class GFG
{
// Function to return the minimum difference
// between N and a power of 2
static int minAbsDiff(int n)
{
// Power of 2 closest to n on its left
int left = 1 << ((int)Math.Floor(Math.Log(n) / Math.Log(2)));
// Power of 2 closest to n on its right
int right = left * 2;
// Return the minimum abs difference
return Math.Min((n - left), (right - n));
}
// Driver code
static public void Main ()
{
int n = 15;
Console.WriteLine(minAbsDiff(n));
}
}
// This code is contributed by jit_t.
的PHP
输出:
1