给定整数N。任务是在给定操作的最小数量中将给定数量N减少为1 。您可以在每个步骤中执行以下任一操作。
- 如果数字是偶数,则可以将数字除以2 。
- 如果数字为奇数,则允许您执行(N + 1)或(N – 1) 。
该任务是通过执行上述操作来打印将N减少为1所需的最少步骤数。
例子:
Input: N = 15
Output: 5
15 is odd 15 + 1 = 16
16 is even 16 / 2 = 8
8 is even 8 / 2 = 4
4 is even 4 / 2 = 2
2 is even 2 / 2 = 1
Input: N = 4
Output: 2
方法:本文已经讨论了解决上述问题的递归方法。在本文中,将讨论一种甚至优化的方法。
解决方案的第一步是要意识到,只有在LSB为零的情况下(即第一种类型的运算),才可以删除它。现在,奇数呢。可能有人认为您只需要删除尽可能多的1即可增加不正确数字的均匀度,例如:
111011 -> 111010 -> 11101 -> 11100 -> 1110 -> 111 -> 1000 -> 100 -> 10 -> 1
但是,这不是最好的方法,因为
111011 -> 111100 -> 11110 -> 1111 -> 10000 -> 1000 -> 100 -> 10 -> 1
111011-> 111010和111011-> 111100都删除了相同的1,但是第二种方法更好。
因此,必须删除最大的1,在n = 3的情况下,如果出现平局,则在+1的情况下执行+1会失败,因为11-> 10-> 1优于11-> 100-> 10-> 1 。幸运的是,这是唯一的例外。
因此逻辑是:
- 如果N是偶数。
- 执行第一个操作,即被2除。
- 如果N为奇数。
- 如果N = 3或(N – 1)的1数少于(N + 1)的数。
- 递减N。
- 别的
- 增量N。
- 如果N = 3或(N – 1)的1数少于(N + 1)的数。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
// Function to return the number
// of set bits in n
int set_bits(int n)
{
int count = 0;
while (n) {
count += n % 2;
n /= 2;
}
return count;
}
// Function to return the minimum
// steps required to reach 1
int minSteps(int n)
{
int ans = 0;
while (n != 1) {
// If n is even then divide it by 2
if (n % 2 == 0)
n /= 2;
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
or set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
int main()
{
int n = 15;
cout << minSteps(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the number
// of set bits in n
static int set_bits(int n)
{
int count = 0;
while (n > 0)
{
count += n % 2;
n /= 2;
}
return count;
}
// Function to return the minimum
// steps required to reach 1
static int minSteps(int n)
{
int ans = 0;
while (n != 1)
{
// If n is even then divide it by 2
if (n % 2 == 0)
n /= 2;
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
|| set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 15;
System.out.print(minSteps(n));
}
}
// This code is contributed by PrinciRaj1992
Python
# Python3 implementation of the approach
# Function to return the number
# of set bits in n
def set_bits(n):
count = 0
while (n):
count += n % 2
n //= 2
return count
# Function to return the minimum
# steps required to reach 1
def minSteps(n):
ans = 0
while (n != 1):
# If n is even then divide it by 2
if (n % 2 == 0):
n //= 2
# If n is 3 or the number of set bits
# in (n - 1) is less than the number
# of set bits in (n + 1)
elif (n == 3 or set_bits(n - 1) < set_bits(n + 1)):
n -= 1
else:
n += 1
# Increment the number of steps
ans += 1
# Return the minimum number of steps
return ans
# Driver code
n = 15
print(minSteps(n))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the number
// of set bits in n
static int set_bits(int n)
{
int count = 0;
while (n > 0)
{
count += n % 2;
n /= 2;
}
return count;
}
// Function to return the minimum
// steps required to reach 1
static int minSteps(int n)
{
int ans = 0;
while (n != 1)
{
// If n is even then divide it by 2
if (n % 2 == 0)
n /= 2;
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
|| set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
public static void Main(String[] args)
{
int n = 15;
Console.Write(minSteps(n));
}
}
// This code is contributed by Rajput-Ji
5