📌  相关文章
📜  通过执行给定的操作将数字减少为1

📅  最后修改于: 2021-04-29 12:41:56             🧑  作者: Mango

给定数字N。任务是以最少的步骤数将给定数字N减小为1。您可以在每个步骤中执行以下任一操作。

  • 操作1 :如果数字为偶数,则可以将数字除以2。
  • 操作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 = 7
Output : 4
    7->6    
    6->3 
    3->2    
    2->1

方法1 –
想法是递归计算所需的最小步骤数。

  • 如果数字是偶数,那么我们只能将数字除以2。
  • 但是,当数字为奇数时,我们可以将其递增或递减1。因此,我们将对n-1和n + 1都使用递归,并以最少的操作数返回一个。

下面是上述方法的实现:

C++
// C++ program to count minimum
// steps to reduce a number
#include 
#include 
 
using namespace std;
 
int countways(int n)
{
    if (n == 1)
        return 0;
    else if (n % 2 == 0)
        return 1 + countways(n / 2);
    else
        return 1 + min(countways(n - 1),
                       countways(n + 1));
}
 
// Driver code
int main()
{
    int n = 15;
 
    cout << countways(n) << "\n";
 
    return 0;
}


Java
// Java program to count minimum
// steps to reduce a number
class Geeks {
 
    static int countways(int n)
    {
        if (n == 1)
            return 0;
        else if (n % 2 == 0)
            return 1 + countways(n / 2);
        else
            return 1 + Math.min(countways(n - 1), countways(n + 1));
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 15;
 
        System.out.println(countways(n));
    }
}
 
// This code is contributed by ankita_saini


Python3
# Python3 program to count minimum
# steps to reduce a number
 
 
def countways(n):
    if (n == 1):
        return 0;
    elif (n % 2 == 0):
        return 1 + countways(n / 2);
    else:
        return 1 + min(countways(n - 1),
                    countways(n + 1));
 
# Driver code
n = 15;
print(countways(n));
 
# This code is contributed by PrinciRaj1992


C#
// C# program to count minimum
// steps to reduce a number
using System;
 
class GFG {
    static int countways(int n)
    {
        if (n == 1)
            return 0;
        else if (n % 2 == 0)
            return 1 + countways(n / 2);
        else
            return 1 + Math.Min(countways(n - 1), countways(n + 1));
    }
 
    // Driver code
    static public void Main()
    {
        int n = 15;
        Console.Write(countways(n));
    }
}
 
// This code is contributed by Raj


C++
// C++ program for the above approach
#include 
using namespace std;
 
int countSteps(int n)
{
    int count = 0;
    while (n > 1) {
        count++;
 
        // num even, divide by 2
        if (n % 2 == 0)
            n /= 2;
 
        // num odd, n%4 == 1
        // or n==3(special edge case),
        // decrement by 1
        else if (n % 4 == 1||n==3)
            n -= 1;
 
        // num odd, n%4 == 3, increment by 1
        else
            n += 1;
    }
 
    return count;
}
 
// driver code
 
int main()
{
    int n = 15;
 
    // Function call
    cout << countSteps(n) << "\n";
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
 
public static int countSteps(int n)
{
    int count = 0;
     
    while (n > 1)
    {
        count++;
         
        // num even, divide by 2
        if (n % 2 == 0)
            n /= 2;
             
        // num odd, n%4 == 1
        // or n==3(special edge case),
        // decrement by 1
        else if (n % 4 == 1||n==3)
            n -= 1;
             
        // num odd, n%4 == 3, increment by 1
        else
            n += 1;
    }
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 15;
     
    // Function call
    System.out.print(countSteps(n));
}
}
 
// This code is contributed by paragpallavsingh


Python3
# Python3 program for the above approach
def countSteps(n):
     
    count = 0
    while (n > 1):
        count += 1
 
        # num even, divide by 2
        if (n % 2 == 0):
            n //= 2
 
        # num odd, n%4 == 1
        # or n==3(special edge case),
        # decrement by 1
        elif (n % 4 == 1 or n == 3):
            n -= 1
 
        # num odd, n%4 == 3, increment by 1
        else:
            n += 1
 
    return count
 
# Driver code
if __name__ == "__main__":
     
    n = 15
 
    # Function call
    print(countSteps(n))
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
     
public static int countSteps(int n)
{
    int count = 0;
      
    while (n > 1)
    {
        count++;
          
        // num even, divide by 2
        if (n % 2 == 0)
            n /= 2;
              
        // num odd, n%4 == 1
        // or n==3(special edge case),
        // decrement by 1
        else if (n % 4 == 1||n==3)
            n -= 1;
              
        // num odd, n%4 == 3, increment by 1
        else
            n += 1;
    }
    return count;
}
  
// Driver code
static public void Main ()
{
    int n = 15;
  
    // Function call   
    Console.WriteLine(countSteps(n));
}
}
 
// This code is contributed by avanitrachhadiya2155


输出:
5

上述方法的时间复杂度为O(2 ^ n)。可以将这种复杂度降低为O(log n)。

方法2 –(有效解决方案)
几乎没有观察到,很明显,对奇数执行1的增减或1的减1可以得到偶数,其中之一可被4整除。对于奇数,唯一的可能运算是1或1的增量。减1,最肯定的是,一个操作将导致整数被四整除,这显然是最佳选择。

Algorithm : 
1. Initialize count = 0
2. While number is greater than one perform following steps - 
         Perform count++ for each iteration
         if num % 2 == 0, perform division
         else if num % 4 == 3, perform increment
         else perform decrement (as odd % 4 is either 1 or 3)
3. return count;

C++

// C++ program for the above approach
#include 
using namespace std;
 
int countSteps(int n)
{
    int count = 0;
    while (n > 1) {
        count++;
 
        // num even, divide by 2
        if (n % 2 == 0)
            n /= 2;
 
        // num odd, n%4 == 1
        // or n==3(special edge case),
        // decrement by 1
        else if (n % 4 == 1||n==3)
            n -= 1;
 
        // num odd, n%4 == 3, increment by 1
        else
            n += 1;
    }
 
    return count;
}
 
// driver code
 
int main()
{
    int n = 15;
 
    // Function call
    cout << countSteps(n) << "\n";
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
 
public static int countSteps(int n)
{
    int count = 0;
     
    while (n > 1)
    {
        count++;
         
        // num even, divide by 2
        if (n % 2 == 0)
            n /= 2;
             
        // num odd, n%4 == 1
        // or n==3(special edge case),
        // decrement by 1
        else if (n % 4 == 1||n==3)
            n -= 1;
             
        // num odd, n%4 == 3, increment by 1
        else
            n += 1;
    }
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 15;
     
    // Function call
    System.out.print(countSteps(n));
}
}
 
// This code is contributed by paragpallavsingh

Python3

# Python3 program for the above approach
def countSteps(n):
     
    count = 0
    while (n > 1):
        count += 1
 
        # num even, divide by 2
        if (n % 2 == 0):
            n //= 2
 
        # num odd, n%4 == 1
        # or n==3(special edge case),
        # decrement by 1
        elif (n % 4 == 1 or n == 3):
            n -= 1
 
        # num odd, n%4 == 3, increment by 1
        else:
            n += 1
 
    return count
 
# Driver code
if __name__ == "__main__":
     
    n = 15
 
    # Function call
    print(countSteps(n))
 
# This code is contributed by chitranayal

C#

// C# program for the above approach
using System;
 
class GFG{
     
public static int countSteps(int n)
{
    int count = 0;
      
    while (n > 1)
    {
        count++;
          
        // num even, divide by 2
        if (n % 2 == 0)
            n /= 2;
              
        // num odd, n%4 == 1
        // or n==3(special edge case),
        // decrement by 1
        else if (n % 4 == 1||n==3)
            n -= 1;
              
        // num odd, n%4 == 3, increment by 1
        else
            n += 1;
    }
    return count;
}
  
// Driver code
static public void Main ()
{
    int n = 15;
  
    // Function call   
    Console.WriteLine(countSteps(n));
}
}
 
// This code is contributed by avanitrachhadiya2155
输出
5

时间复杂度: O(logN)