Given two integers A and B, the task is to print the integer among the two, which will be converted to an odd integer by a smaller number of divisions by 2. If both the numbers get converted to an odd integer after the same number of operations, print -1.
Examples:
Input: A = 10 and B = 8
Output: 10
Explanation:
Step 1: A/2 = 5, B/2 = 4
Hence, A is first number to be converted to an odd integer.Input: A = 20 and B = 12
Output: -1
Explanation:
Step 1: A/2 = 10, B/2 = 6
Step 2: A/2 = 5, B/2 = 3
Hence, A and B are converted to an odd integer at the same time.
Naive Approach:
The simplest approach to solve the problem is as follows:
- Check if any of the two given numbers are even or odd. If one of them is odd, print that number.
- If both are odd, print -1.
- Otherwise, keep dividing both of them by 2 at each step and check if any of them is converted to an odd integer or not. If one of them is converted, print the initial value of that number. If both are converted at the same time print -1.
Below is the implementation of the above approach:
C++
// C++ program to find the first // number to be converted to an odd // integer by repetitive division by 2 #include <bits/stdc++.h> 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, 8print(odd_first(a, b)) |
10
Time complexity: O(log(min(a, b)))
Auxiliary Space: O(1)
Efficient Approach:
Follow the steps below to optimize the above approach:
- Dividing a number by 2 is equivalent to perform right shift on that number.
- Hence, the number with the Least Significant Set Bit among the two will be the first to be converted to an odd integer.
Below is the implementation of the above approach.
C++
// C++ Program to implement the // above approach #include <bits/stdc++.h> 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 |
10
Time complexity: O(1)
Auxiliary Space: O(1)
Recommended Posts:
- Find Quotient and Remainder of two integer without using division operators
- Program for quotient and remainder of big number
- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Find the number after successive division
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Compute modulus division by a power-of-2-number
- Number of digits before the decimal point in the division of two numbers
- Breaking a number such that first part is integral division of second by a power of 10
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Find the remainder when First digit of a number is divided by its Last digit
- Remainder with 7 for large numbers
- Find permutation with maximum remainder Sum
- Chinese Remainder Theorem | Set 1 (Introduction)
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Quotient and remainder dividing by 2^k (a power of 2)
- Maximum frequency of a remainder modulo 2i
- Find remainder of array multiplication divided by n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29


