Divide two number using Binary search without using any / and % operator
Given two integers one is a dividend and the other is the divisor, we need to find the quotient when the dividend is divided by the divisor without the use of any ” / “ and ” % “ operators.
Examples:
Input: dividend = 10, divisor = 2
Output: 5
Explanation: 10/2 = 5.
Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333… which is truncated to 3.
Input: dividend = 10, divisor = -2
Output: -5
Explanation: 10/-2 = -5.
Approach: To solve the problem using Binary Search follow the below idea:
We already know that Quotient * Divisor ? Dividend and the Quotient lie between 0 and Dividend. Therefore, we can assume the Quotient as mid, the Dividend as higher bound and 0 as the lower bound and can easily use binary search to satisfy the terms of division which is Quotient * Divisor ? Dividend.
Follow the steps to solve the problem:
- At first, set high = dividend and low = 0 .
- Then, we need to find the mid ( i.e Quotient ) = low + (high – low ) / 2 .
- Then, we perform Binary Search in the range of 0 to the dividend.
- If mid * divisor > dividend, then update high = mid – 1 .
- Else we update low = mid + 1
- The value of mid which satisfies the condition (i.e mid * divisor ? dividend) is our Quotient.
- Then, we return it by checking the Parity.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int divide_binary_search( int dividend, int divisor)
{
if (divisor == 1)
return dividend;
if (divisor == -1)
return -dividend;
long long low = 0, high = abs (dividend), mid;
int quotient = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if ( abs (mid * divisor) > abs (dividend))
high = mid - 1;
else {
quotient = mid;
low = mid + 1;
}
}
if ((dividend < 0 && divisor < 0
|| dividend > 0 && divisor > 0))
return quotient;
else
return -quotient;
}
int main()
{
int dividend = 10, divisor = 2;
cout << "The Quotient is : "
<< divide_binary_search(dividend, divisor);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static long divide_binary_search( int dividend,
int divisor)
{
if (divisor == 1 )
return dividend;
if (divisor == - 1 )
return -dividend;
else if (dividend == Integer.MIN_VALUE
&& divisor == - 1 )
return Integer.MAX_VALUE;
long low = 0 , high = Math.abs(dividend), mid = 0 ;
long quotient = 0 ;
while (low <= high) {
mid = low + (high - low) / 2 ;
if (Math.abs(mid * divisor)
> Math.abs(dividend))
high = mid - 1 ;
else {
quotient = mid;
low = mid + 1 ;
}
}
if ((dividend < 0 && divisor < 0
|| dividend > 0 && divisor > 0 ))
return quotient;
else
return -quotient;
}
public static void main(String[] args)
{
int dividend = 10 , divisor = 2 ;
System.out.print(
"The Quotient is : "
+ divide_binary_search(dividend, divisor));
}
}
|
Python3
import math
def divide_binary_search(dividend, divisor):
if (divisor = = 1 ):
return dividend
if (divisor = = - 1 ):
return - dividend
low = 0
high = abs (dividend)
mid = 0
quotient = 0
while (low < = high):
mid = low + math.floor((high - low) / 2 )
if ( abs (mid * divisor) > abs (dividend)):
high = mid - 1
else :
quotient = mid
low = mid + 1
if ((dividend < 0 and divisor < 0 or dividend > 0 and divisor > 0 )):
return quotient
else :
return - quotient
dividend = 10
divisor = 2
print ( "The Quotient is :" , divide_binary_search(dividend, divisor))
|
C#
using System;
public class GFG {
public static int divide_binary_search( int dividend,
int divisor)
{
if (divisor == 1)
return dividend;
if (divisor == -1)
return -dividend;
long low = 0, high = Math.Abs(dividend);
long mid;
int quotient = 0;
while (low <= high) {
mid = low + ((high - low) / 2);
if (Math.Abs(mid * divisor)
> Math.Abs(dividend))
high = mid - 1;
else {
quotient = ( int )mid;
low = mid + 1;
}
}
if ((dividend < 0 && divisor < 0
|| dividend > 0 && divisor > 0))
return quotient;
else
return -quotient;
}
static public void Main()
{
int dividend = 10, divisor = 2;
Console.WriteLine(
"The Quotient is : "
+ divide_binary_search(dividend, divisor));
}
}
|
Javascript
function divide_binary_search(dividend, divisor)
{
if (divisor == 1)
return dividend;
if (divisor == -1)
return -dividend;
let low = 0, high = Math.abs(dividend), mid;
let quotient = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if (Math.abs(mid * divisor) > Math.abs(dividend))
high = mid - 1;
else {
quotient = mid;
low = mid + 1;
}
}
if ((dividend < 0 && divisor < 0
|| dividend > 0 && divisor > 0))
return quotient;
else
return -quotient;
}
let dividend = 10, divisor = 2;
console.log( "The Quotient is : "
+ divide_binary_search(dividend, divisor));
|
Output
The Quotient is : 5
Time Complexity: O(log N), as Binary Search algorithm is used.
Auxiliary Space: O(1), since no extra space has been used.
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...