将 N 减少到 0 所需执行的给定操作的最小数量
给定一个整数N ,任务是使用以下操作任意次数将N减少到 0 的最小操作次数:
- 更改N的二进制表示中最右边的 (0 th ) 位。
- 如果第( i -1)位设置为 1,并且第( i -2)到第0位设置为 0,则更改N的二进制表示中的第i位。
例子:
Input: N = 3
Output: 2
Explanation: The binary representation of 3 is “11”.
“11″ → “01” with the 2nd operation since the 0th bit is 1.
“01″ → “00″ with the 1st operation.
Input: N = 6
Output: 4
Explanation: The binary representation of 6 is “110”.
“110” → “010” with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
“010” → “011” with the 1st operation.
“011” → “001” with the 2nd operation since the 0th bit is 1.
“001” → “000” with the 1st operation.
方法:这个想法基于以下观察:
- 1 → 0 需要 1 次操作。
2 → 0 即10 → 11 → 01 → 0 需要 3 次操作。
4 → 0 即100 → 101 → 111 → 110 → 010 → 11 → 01 →0 需要 7 次操作。
因此,它可以推广到任何 2 的幂,即 2 k需要 2 (k+1) -1 次操作。 - 如果a→b需要k次操作,b→a也需要k次操作。
从 2 n到 2 (n-1) 的中间数包含 2 n到 2 (n- 1 ) 之间的所有数字,这表明任何给定的非负整数都可以转换为 0。设 f(n) 是一个函数找到所需的最小操作数。递归关系为:
f(n) = f(2 k ) – f(n xor 2 k ),其中 k = n 的最高有效位的下一位。
这个想法是使用递归。请按照以下步骤解决问题:
- 创建一个以数字N作为参数的递归函数。
- 如果N的值等于0 ,则返回0 。
- 否则,找到小于或等于N的 2 的最大幂并将其存储在变量X中。
- 将通过递归调用(X^(X/2)^N)的函数返回的值存储在变量S中。
- 将X的值添加到S并返回。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum
// operations required to convert
// N to 0
int minimumOneBitOperations(int n, int res = 0)
{
// Base Case
if (n == 0)
return res;
// Store the highest power of 2
// less than or equal to n
int b = 1;
while ((b << 1) <= n)
b = b << 1;
// Return the result
return minimumOneBitOperations(
(b >> 1) ^ b ^ n, res + b);
}
// Driver Code
int main()
{
// Given Input
int N = 6;
// Function call
cout << minimumOneBitOperations(N);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find minimum
// operations required to convert
// N to 0
public static int minimumOneBitOperations(int n, int res)
{
// Base Case
if (n == 0)
return res;
// Store the highest power of 2
// less than or equal to n
int b = 1;
while ((b << 1) <= n)
b = b << 1;
// Return the result
return minimumOneBitOperations((b >> 1) ^ b ^ n, res + b);
}
// Driver Code
public static void main(String args[])
{
// Given Input
int N = 6;
// Function call
System.out.println(minimumOneBitOperations(N, 0));
}
}
// This code is contributed by gfgking.
Python3
# Python program for the above approach
# Function to find minimum
# operations required to convert
# N to 0
def minimumOneBitOperations(n, res):
# Base case
if n == 0:
return res
# Store the highest power of 2
# less than or equal to n
b = 1
while (b << 1) <= n:
b = b << 1
# Return the result
return minimumOneBitOperations((b >> 1) ^ b ^ n, res + b)
# Driver code
N = 6
print(minimumOneBitOperations(N, 0))
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find minimum
// operations required to convert
// N to 0
static int minimumOneBitOperations(int n, int res)
{
// Base Case
if (n == 0)
return res;
// Store the highest power of 2
// less than or equal to n
int b = 1;
while ((b << 1) <= n)
b = b << 1;
// Return the result
return minimumOneBitOperations(
(b >> 1) ^ b ^ n, res + b);
}
// Driver Code
public static void Main()
{
// Given Input
int N = 6;
// Function call
Console.Write(minimumOneBitOperations(N,0));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
4
时间复杂度: O(log(N))
辅助空间: O(1)