给定两个非负整数N和K ,任务是将N的第K个最高有效位取反并打印取反后获得的数字。
例子:
Input: N = 10, K = 1
Output: 2
The binary representation of 10 is 1010.
After inverting the first bit it becomes 0010
whose decimal equivalent is 2.
Input: N = 56, K = 2
Output: 40
方法:查找N位的号码,如果位的数量少于k个则N本身就是其他所需答案翻转的K第N个最显著位和打印翻转之后所获得的数量。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to convert decimal number n
// to its binary representation
// stored as an array arr[]
void decBinary(int arr[], int n)
{
int k = log2(n);
while (n > 0) {
arr[k--] = n % 2;
n /= 2;
}
}
// Function to convert the number
// represented as a binary array
// arr[] into its decimal equivalent
int binaryDec(int arr[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
// Function to return the updated integer
// after flipping the kth bit
int getNum(int n, int k)
{
// Number of bits in n
int l = log2(n) + 1;
// Find the binary
// representation of n
int a[l] = { 0 };
decBinary(a, n);
// The number of bits in n
// are less than k
if (k > l)
return n;
// Flip the kth bit
a[k - 1] = (a[k - 1] == 0) ? 1 : 0;
// Return the decimal equivalent
// of the number
return binaryDec(a, l);
}
// Driver code
int main()
{
int n = 56, k = 2;
cout << getNum(n, k);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to convert decimal number n
// to its binary representation
// stored as an array arr[]
static void decBinary(int arr[], int n)
{
int k = (int)(Math.log(n) /
Math.log(2));
while (n > 0)
{
arr[k--] = n % 2;
n /= 2;
}
}
// Function to convert the number
// represented as a binary array
// arr[] into its decimal equivalent
static int binaryDec(int arr[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
// Function to return the updated integer
// after flipping the kth bit
static int getNum(int n, int k)
{
// Number of bits in n
int l = (int)(Math.log(n) /
Math.log(2)) + 1;
// Find the binary
// representation of n
int a[] = new int[l];
decBinary(a, n);
// The number of bits in n
// are less than k
if (k > l)
return n;
// Flip the kth bit
a[k - 1] = (a[k - 1] == 0) ? 1 : 0;
// Return the decimal equivalent
// of the number
return binaryDec(a, l);
}
// Driver code
public static void main (String[] args)
{
int n = 56;
int k = 2;
System.out.println(getNum(n, k));
}
}
// This code is contributed by AnkitRai01
Python
# Python implementation of the approach
import math
# Function to convert decimal number n
# to its binary representation
# stored as an array arr[]
def decBinary(arr, n):
k = int(math.log2(n))
while (n > 0):
arr[k] = n % 2
k = k - 1
n = n//2
# Function to convert the number
# represented as a binary array
# arr[] its decimal equivalent
def binaryDec(arr, n):
ans = 0
for i in range(0, n):
ans = ans + (arr[i] << (n - i - 1))
return ans
# Function to concatenate the binary
# numbers and return the decimal result
def getNum(n, k):
# Number of bits in both the numbers
l = int(math.log2(n)) + 1
# Convert the bits in both the gers
# to the arrays a[] and b[]
a = [0 for i in range(0, l)]
decBinary(a, n)
# The number of bits in n
# are less than k
if(k > l):
return n
# Flip the kth bit
if(a[k - 1] == 0):
a[k - 1] = 1
else:
a[k - 1] = 0
# Return the decimal equivalent
# of the number
return binaryDec(a, l)
# Driver code
n = 56
k = 2
print(getNum(n, k))
# This code is contributed by Sanjit_Prasad
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to convert decimal number n
// to its binary representation
// stored as an array []arr
static void decBinary(int []arr, int n)
{
int k = (int)(Math.Log(n) /
Math.Log(2));
while (n > 0)
{
arr[k--] = n % 2;
n /= 2;
}
}
// Function to convert the number
// represented as a binary array
// []arr into its decimal equivalent
static int binaryDec(int []arr, int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
// Function to return the updated integer
// after flipping the kth bit
static int getNum(int n, int k)
{
// Number of bits in n
int l = (int)(Math.Log(n) /
Math.Log(2)) + 1;
// Find the binary
// representation of n
int []a = new int[l];
decBinary(a, n);
// The number of bits in n
// are less than k
if (k > l)
return n;
// Flip the kth bit
a[k - 1] = (a[k - 1] == 0) ? 1 : 0;
// Return the decimal equivalent
// of the number
return binaryDec(a, l);
}
// Driver code
public static void Main(String[] args)
{
int n = 56;
int k = 2;
Console.WriteLine(getNum(n, k));
}
}
// This code is contributed by PrinciRaj1992
输出:
40