First number to leave an odd remainder after repetitive division by 2
Last Updated :
26 Nov, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int odd_first( int a, int b)
{
int true_a = a;
int true_b = b;
while (a % 2 != 1 && b % 2 != 1)
{
a = a / 2;
b = b / 2;
}
if (a % 2 == 1 && b % 2 == 1)
return -1;
else if (a % 2 == 1)
return true_a;
else
return true_b;
}
int main()
{
int a = 10;
int b = 8;
cout << odd_first(a, b);
}
|
Java
import java.util.*;
import java.lang.Math;
import java.io.*;
class GFG{
static int odd_first( int a, int b)
{
int true_a = a;
int true_b = b;
while (a % 2 != 1 && b % 2 != 1 )
{
a = a / 2 ;
b = b / 2 ;
}
if (a % 2 == 1 && b % 2 == 1 )
return - 1 ;
else if (a % 2 == 1 )
return true_a;
else
return true_b;
}
public static void main(String[] args)
{
int a = 10 ;
int b = 8 ;
System.out.print(odd_first(a, b));
}
}
|
Python3
def odd_first(a, b):
true_a = a
true_b = b
while (a % 2 ! = 1 and b % 2 ! = 1 ):
a = a / / 2
b = b / / 2
if a % 2 = = 1 and b % 2 = = 1 :
return - 1
elif a % 2 = = 1 :
return true_a
else :
return true_b
a, b = 10 , 8
print (odd_first(a, b))
|
C#
using System;
class GFG{
static int odd_first( int a, int b)
{
int true_a = a;
int true_b = b;
while (a % 2 != 1 && b % 2 != 1)
{
a = a / 2;
b = b / 2;
}
if (a % 2 == 1 && b % 2 == 1)
return -1;
else if (a % 2 == 1)
return true_a;
else
return true_b;
}
public static void Main()
{
int a = 10;
int b = 8;
Console.Write(odd_first(a, b));
}
}
|
Javascript
<script>
function odd_first(a, b)
{
var true_a = a;
var true_b = b;
while (a % 2 != 1 && b % 2 != 1)
{
a = a / 2;
b = b / 2;
}
if (a % 2 == 1 && b % 2 == 1)
return -1;
else if (a % 2 == 1)
return true_a;
else
return true_b;
}
var a = 10, b = 8;
document.write(odd_first(a, b));
</script>
|
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 the 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++
#include <bits/stdc++.h>
using namespace std;
int getFirstSetBitPos( int n)
{
return log2(n & -n) + 1;
}
int oddFirst( int a, int b)
{
int steps_a = getFirstSetBitPos(a);
int steps_b = getFirstSetBitPos(b);
if (steps_a == steps_b) {
return -1;
}
if (steps_a > steps_b) {
return b;
}
if (steps_a < steps_b) {
return a;
}
}
int main()
{
int a = 10;
int b = 8;
cout << oddFirst(a, b);
}
|
Java
import java.util.*;
import java.lang.Math;
import java.io.*;
class GFG{
static int getFirstSetBitPos( int n)
{
return ( int )(Math.log(n & -n) /
Math.log( 2 ));
}
static int oddFirst( int a, int b)
{
int steps_a = getFirstSetBitPos(a);
int steps_b = getFirstSetBitPos(b);
if (steps_a == steps_b)
{
return - 1 ;
}
else if (steps_a > steps_b)
{
return b;
}
else
{
return a;
}
}
public static void main(String[] args)
{
int a = 10 ;
int b = 8 ;
System.out.print(oddFirst(a, b));
}
}
|
Python3
from math import log
def getFirstSetBitPos(n):
return log(n & - n, 2 ) + 1
def oddFirst(a, b):
steps_a = getFirstSetBitPos(a)
steps_b = getFirstSetBitPos(b)
if (steps_a = = steps_b):
return - 1
if (steps_a > steps_b):
return b
if (steps_a < steps_b):
return a
if __name__ = = '__main__' :
a = 10
b = 8
print (oddFirst(a, b))
|
C#
using System;
class GFG{
static int getFirstSetBitPos( int n)
{
return ( int )(Math.Log(n & -n) /
Math.Log(2));
}
static int oddFirst( int a, int b)
{
int steps_a = getFirstSetBitPos(a);
int steps_b = getFirstSetBitPos(b);
if (steps_a == steps_b)
{
return -1;
}
else if (steps_a > steps_b)
{
return b;
}
else
{
return a;
}
}
public static void Main()
{
int a = 10;
int b = 8;
Console.Write(oddFirst(a, b));
}
}
|
Javascript
<script>
function getFirstSetBitPos(n)
{
return (Math.log(n & -n) /
Math.log(2));
}
function oddFirst(a, b)
{
let steps_a = getFirstSetBitPos(a);
let steps_b = getFirstSetBitPos(b);
if (steps_a == steps_b)
{
return -1;
}
else if (steps_a > steps_b)
{
return b;
}
else
{
return a;
}
}
let a = 10;
let b = 8;
document.write(oddFirst(a, b));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...