📌  相关文章
📜  重复除以2后的第一个留下奇数余数的数字

📅  最后修改于: 2021-04-23 22:14:28             🧑  作者: Mango

给定两个整数AB ,任务是在这两个整数之间打印整数,该整数将被较小的除以2的结果转换为奇数整数。如果两个数字在相同的整数后都转换为奇数整数操作,打印-1。

例子:

天真的方法:
解决问题的最简单方法如下:

  • 检查两个给定数字中的任何一个是偶数还是奇数。如果其中之一是奇数,请打印该号码。
  • 如果两者都不相同,请打印-1。
  • 否则,请在每个步骤中将它们都除以2,然后检查是否将它们中的任何一个转换为奇数整数。如果其中之一被转换,则打印该数字的初始值。如果两者都同时转换,则打印-1。

下面是上述方法的实现:

C++
// C++ program to find the first
// number to be converted to an odd
// integer by repetitive division by 2
#include 
using namespace std;
 
// Function to return the first number
// to to be converted to an odd value
int odd_first(int a, int b)
{
     
    // Initial values
    int true_a = a;
    int true_b = b;
 
    // Perform repetitive divisions by 2
    while(a % 2 != 1 && b % 2 != 1)
    {
        a = a / 2;
        b = b / 2;
    }
 
    // If both become odd at same step
    if (a % 2 == 1 && b % 2 == 1)
        return -1;
 
    // If a is first to become odd
    else if (a % 2 == 1)
        return true_a;
 
    // If b is first to become odd
    else
        return true_b;
}
         
// Driver code
int main()
{
    int a = 10;
    int b = 8;
 
    cout << odd_first(a, b);
}
 
// This code is contributed by code_hunt


Java
// Java program to find the first
// number to be converted to an odd
// integer by repetitive division by 2
import java.util.*;
import java.lang.Math;
import java.io.*;
 
class GFG{
     
// Function to return the first number
// to to be converted to an odd value
static int odd_first(int a, int b)
{
     
    // Initial values
    int true_a = a;
    int true_b = b;
 
    // Perform repetitive divisions by 2
    while(a % 2 != 1 && b % 2 != 1)
    {
        a = a / 2;
        b = b / 2;
    }
 
    // If both become odd at same step
    if (a % 2 == 1 && b % 2 == 1)
        return -1;
 
    // If a is first to become odd
    else if (a % 2 == 1)
        return true_a;
 
    // If b is first to become odd
    else
        return true_b;
}
     
// Driver code
public static void main(String[] args)
{
    int a = 10;
    int b = 8;
 
    System.out.print(odd_first(a, b));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to find the first
# number to be converted to an odd
# integer by repetitive division by 2
 
# Function to return the first number
# to to be converted to an odd value
def odd_first(a, b):
 
  # Initial values
  true_a = a
  true_b = b
 
  # Perform repetitive divisions by 2
  while(a % 2 != 1 and b % 2 != 1):
    a = a//2
    b = b//2
 
  # If both become odd at same step
  if a % 2 == 1 and b % 2 == 1:
    return -1
 
  # If a is first to become odd
  elif a % 2 == 1:
    return true_a
 
  # If b is first to become odd
  else:
    return true_b
 
# Driver Code
a, b = 10, 8
print(odd_first(a, b))


C#
// C# program to find the first
// number to be converted to an odd
// integer by repetitive division by 2
using System;
 
class GFG{
     
// Function to return the first number
// to to be converted to an odd value
static int odd_first(int a, int b)
{
     
    // Initial values
    int true_a = a;
    int true_b = b;
 
    // Perform repetitive divisions by 2
    while(a % 2 != 1 && b % 2 != 1)
    {
        a = a / 2;
        b = b / 2;
    }
 
    // If both become odd at same step
    if (a % 2 == 1 && b % 2 == 1)
        return -1;
 
    // If a is first to become odd
    else if (a % 2 == 1)
        return true_a;
 
    // If b is first to become odd
    else
        return true_b;
}
     
// Driver code
public static void Main()
{
    int a = 10;
    int b = 8;
 
    Console.Write(odd_first(a, b));
}
}
 
// This code is contributed by sanjoy_62


Javascript


C++
// C++ Program to implement the
// above approach
#include 
using namespace std;
 
// Function to return the position
// least significant set bit
int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Function return the first number
// to be converted to an odd integer
int oddFirst(int a, int b)
{
 
    // Stores the positions of the
    // first set bit
    int steps_a = getFirstSetBitPos(a);
    int steps_b = getFirstSetBitPos(b);
 
    // If both are same
    if (steps_a == steps_b) {
        return -1;
    }
 
    // If A has the least significant
    // set bit
    if (steps_a > steps_b) {
        return b;
    }
 
    // Otherwise
    if (steps_a < steps_b) {
        return a;
    }
}
 
// Driver code
int main()
{
    int a = 10;
    int b = 8;
 
    cout << oddFirst(a, b);
}


Java
// Java program implementation
// of the approach
import java.util.*;
import java.lang.Math;
import java.io.*;
 
class GFG{
     
// Function to return the position
// least significant set bit
static int getFirstSetBitPos(int n)
{
    return (int)(Math.log(n & -n) /
                 Math.log(2));
}
 
// Function return the first number
// to be converted to an odd integer
static int oddFirst(int a, int b)
{
     
    // Stores the positions of the
    // first set bit
    int steps_a = getFirstSetBitPos(a);
    int steps_b = getFirstSetBitPos(b);
 
    // If both are same
    if (steps_a == steps_b)
    {
        return -1;
    }
 
    // If A has the least significant
    // set bit
    else if (steps_a > steps_b)
    {
        return b;
    }
 
    // Otherwise
    else
    {
        return a;
    }
}
     
// Driver code
public static void main(String[] args)
{
    int a = 10;
    int b = 8;
 
    System.out.print(oddFirst(a, b));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement the
# above approach
from math import log
 
# Function to return the position
# least significant set bit
def getFirstSetBitPos(n):
    return log(n & -n, 2) + 1
 
# Function return the first number
# to be converted to an odd integer
def oddFirst(a, b):
 
    # Stores the positions of the
    # first set bit
    steps_a = getFirstSetBitPos(a)
    steps_b = getFirstSetBitPos(b)
 
    # If both are same
    if (steps_a == steps_b):
        return -1
 
    # If A has the least significant
    # set bit
    if (steps_a > steps_b):
        return b
 
    # Otherwise
    if (steps_a < steps_b):
        return a
 
# Driver code
if __name__ == '__main__':
     
    a = 10
    b = 8
     
    print(oddFirst(a, b))
 
# This code is contributed by mohit kumar 29


C#
// C# program implementation
// of the approach
using System;
 
class GFG{
     
// Function to return the position
// least significant set bit
static int getFirstSetBitPos(int n)
{
    return (int)(Math.Log(n & -n) /
                 Math.Log(2));
}
 
// Function return the first number
// to be converted to an odd integer
static int oddFirst(int a, int b)
{
     
    // Stores the positions of the
    // first set bit
    int steps_a = getFirstSetBitPos(a);
    int steps_b = getFirstSetBitPos(b);
 
    // If both are same
    if (steps_a == steps_b)
    {
        return -1;
    }
 
    // If A has the least significant
    // set bit
    else if (steps_a > steps_b)
    {
        return b;
    }
 
    // Otherwise
    else
    {
        return a;
    }
}
     
// Driver code
public static void Main()
{
    int a = 10;
    int b = 8;
 
    Console.Write(oddFirst(a, b));
}
}
 
// This code is contributed by sanjoy_62


输出:
10

时间复杂度: O(log(min(a,b)))
辅助空间: O(1)

高效方法:
请按照以下步骤优化上述方法:

  • 将数字除以2等效于对该数字执行右移。
  • 因此,两者中具有最低有效位的数字将是第一个被转换为奇数整数的数字。

下面是上述方法的实现。

C++

// C++ Program to implement the
// above approach
#include 
using namespace std;
 
// Function to return the position
// least significant set bit
int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Function return the first number
// to be converted to an odd integer
int oddFirst(int a, int b)
{
 
    // Stores the positions of the
    // first set bit
    int steps_a = getFirstSetBitPos(a);
    int steps_b = getFirstSetBitPos(b);
 
    // If both are same
    if (steps_a == steps_b) {
        return -1;
    }
 
    // If A has the least significant
    // set bit
    if (steps_a > steps_b) {
        return b;
    }
 
    // Otherwise
    if (steps_a < steps_b) {
        return a;
    }
}
 
// Driver code
int main()
{
    int a = 10;
    int b = 8;
 
    cout << oddFirst(a, b);
} 

Java

// Java program implementation
// of the approach
import java.util.*;
import java.lang.Math;
import java.io.*;
 
class GFG{
     
// Function to return the position
// least significant set bit
static int getFirstSetBitPos(int n)
{
    return (int)(Math.log(n & -n) /
                 Math.log(2));
}
 
// Function return the first number
// to be converted to an odd integer
static int oddFirst(int a, int b)
{
     
    // Stores the positions of the
    // first set bit
    int steps_a = getFirstSetBitPos(a);
    int steps_b = getFirstSetBitPos(b);
 
    // If both are same
    if (steps_a == steps_b)
    {
        return -1;
    }
 
    // If A has the least significant
    // set bit
    else if (steps_a > steps_b)
    {
        return b;
    }
 
    // Otherwise
    else
    {
        return a;
    }
}
     
// Driver code
public static void main(String[] args)
{
    int a = 10;
    int b = 8;
 
    System.out.print(oddFirst(a, b));
}
}
 
// This code is contributed by code_hunt

Python3

# Python3 program to implement the
# above approach
from math import log
 
# Function to return the position
# least significant set bit
def getFirstSetBitPos(n):
    return log(n & -n, 2) + 1
 
# Function return the first number
# to be converted to an odd integer
def oddFirst(a, b):
 
    # Stores the positions of the
    # first set bit
    steps_a = getFirstSetBitPos(a)
    steps_b = getFirstSetBitPos(b)
 
    # If both are same
    if (steps_a == steps_b):
        return -1
 
    # If A has the least significant
    # set bit
    if (steps_a > steps_b):
        return b
 
    # Otherwise
    if (steps_a < steps_b):
        return a
 
# Driver code
if __name__ == '__main__':
     
    a = 10
    b = 8
     
    print(oddFirst(a, b))
 
# This code is contributed by mohit kumar 29

C#

// C# program implementation
// of the approach
using System;
 
class GFG{
     
// Function to return the position
// least significant set bit
static int getFirstSetBitPos(int n)
{
    return (int)(Math.Log(n & -n) /
                 Math.Log(2));
}
 
// Function return the first number
// to be converted to an odd integer
static int oddFirst(int a, int b)
{
     
    // Stores the positions of the
    // first set bit
    int steps_a = getFirstSetBitPos(a);
    int steps_b = getFirstSetBitPos(b);
 
    // If both are same
    if (steps_a == steps_b)
    {
        return -1;
    }
 
    // If A has the least significant
    // set bit
    else if (steps_a > steps_b)
    {
        return b;
    }
 
    // Otherwise
    else
    {
        return a;
    }
}
     
// Driver code
public static void Main()
{
    int a = 10;
    int b = 8;
 
    Console.Write(oddFirst(a, b));
}
}
 
// This code is contributed by sanjoy_62
输出:
10

时间复杂度: O(1)
辅助空间: O(1)