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.
Input: A = 10 and B = 8
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
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.
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:
Time complexity: O(log(min(a, b)))
Auxiliary Space: O(1)
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.
Time complexity: O(1)
Auxiliary Space: O(1)
- Find Quotient and Remainder of two integer without using division operators
- 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
- Find the number after successive division
- Breaking a number such that first part is integral division of second by a power of 10
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Program for quotient and remainder of big number
- Program to find remainder when large number is divided by 11
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Program to find remainder when large number is divided by r
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Average of odd numbers till a given odd number
- Split the array into odd number of segments of odd lengths
- Check if a number has an odd count of odd divisors and even count of even divisors
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Number of digits before the decimal point in the division of two numbers
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.