Given two integers say a and b. Find the quotient after dividing a by b without using multiplication, division, and mod operator.
Example:
Input : a = 10, b = 3
Output : 3
Input : a = 43, b = -8
Output : -5
Approach: Keep subtracting the divisor from the dividend until the dividend becomes less than the divisor. The dividend becomes the remainder, and the number of times subtraction is done becomes the quotient. Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
long long divide( long long dividend, long long int divisor)
{
long long sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
dividend = abs (dividend);
divisor = abs (divisor);
long long quotient = 0;
while (dividend >= divisor) {
dividend -= divisor;
++quotient;
}
return quotient * sign;
}
int main()
{
int a = -2147483648, b = -1;
cout << divide(a, b) << "\n" ;
a = 43, b = -8;
cout << divide(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long divide( long dividend, long divisor)
{
long sign
= ((dividend < 0 ) ^ (divisor < 0 )) ? - 1 : 1 ;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
long quotient = 0 ;
while (dividend >= divisor) {
dividend -= divisor;
++quotient;
}
if (sign == - 1 )
quotient = -quotient;
return quotient;
}
public static void main(String[] args)
{
int a = - 2147483648 ;
int b = - 1 ;
System.out.println(divide(a, b));
a = 43 ;
b = - 8 ;
System.out.println(divide(a, b));
}
}
|
Python3
def divide(dividend, divisor):
sign = - 1 if ((dividend < 0 ) ^ (divisor < 0 )) else 1
dividend = abs (dividend)
divisor = abs (divisor)
quotient = 0
while (dividend > = divisor):
dividend - = divisor
quotient + = 1
if sign = = - 1 :
quotient = - quotient
return quotient
a = 10
b = 3
print (divide(a, b))
a = 43
b = - 8
print (divide(a, b))
|
C#
using System;
class GFG {
static int divide( int dividend, int divisor)
{
int sign
= ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
dividend = Math.Abs(dividend);
divisor = Math.Abs(divisor);
int quotient = 0;
while (dividend >= divisor) {
dividend -= divisor;
++quotient;
}
if (sign == -1)
quotient = -quotient;
return quotient;
}
public static void Main()
{
int a = 10;
int b = 3;
Console.WriteLine(divide(a, b));
a = 43;
b = -8;
Console.WriteLine(divide(a, b));
}
}
|
PHP
<?php
function divide( $dividend , $divisor ) {
$sign = (( $dividend < 0) ^
( $divisor < 0)) ? -1 : 1;
$dividend = abs ( $dividend );
$divisor = abs ( $divisor );
$quotient = 0;
while ( $dividend >= $divisor )
{
$dividend -= $divisor ;
++ $quotient ;
}
if ( $sign ==-1) $quotient =- $quotient ;
return $quotient ;
}
$a = 10;
$b = 3;
echo divide( $a , $b ). "\n" ;
$a = 43;
$b = -8;
echo divide( $a , $b );
?>
|
Javascript
<script>
function divide(dividend, divisor) {
let sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
let quotient = 0;
while (dividend >= divisor) {
dividend -= divisor;
++quotient;
}
if (sign==-1) quotient=-quotient;
return quotient;
}
let a = 10, b = 3;
document.write(divide(a, b) + "<br>" );
a = 43, b = -8;
document.write(divide(a, b));
</script>
|
Time complexity : O(a/b)
Auxiliary space : O(1)
Efficient Approach: Use bit manipulation in order to find the quotient. The divisor and dividend can be written as
dividend = quotient * divisor + remainder
As every number can be represented in base 2(0 or 1), represent the quotient in binary form by using the shift operator as given below:
- Determine the most significant bit in the divisor. This can easily be calculated by iterating on the bit position i from 31 to 1.
- Find the first bit for which divisor << i is less than dividend and keep updating the ith bit position for which it is true.
- Add the result in the temp variable for checking the next position such that (temp + (divisor << i) ) is less than the dividend.
- Return the final answer of the quotient after updating with a corresponding sign.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
long long divide( long long dividend, long long divisor) {
int sign = ((dividend < 0) ^
(divisor < 0)) ? -1 : 1;
dividend = abs (dividend);
divisor = abs (divisor);
long long quotient = 0, temp = 0;
for ( int i = 31; i >= 0; --i) {
if (temp + (divisor << i) <= dividend) {
temp += divisor << i;
quotient |= 1LL << i;
}
}
if (sign==-1) quotient=-quotient;
return quotient;
}
int main() {
int a = -2147483648, b = -1;
cout << divide(a, b) << "\n" ;
a = 43, b = -8;
cout << divide(a, b);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static long divide( long dividend,
long divisor)
{
long sign = ((dividend < 0 ) ^
(divisor < 0 )) ? - 1 : 1 ;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
long quotient = 0 , temp = 0 ;
for ( int i = 31 ; i >= 0 ; --i)
{
if (temp + (divisor << i) <= dividend)
{
temp += divisor << i;
quotient |= 1L << i;
}
}
if (sign==- 1 )
quotient=-quotient;
return quotient;
}
public static void main(String args[])
{
int a = 10 , b = 3 ;
System.out.println(divide(a, b));
int a1 = 43 , b1 = - 8 ;
System.out.println(divide(a1, b1));
}
}
|
Python3
def divide(dividend, divisor):
sign = ( - 1 if ((dividend < 0 ) ^
(divisor < 0 )) else 1 );
dividend = abs (dividend);
divisor = abs (divisor);
quotient = 0 ;
temp = 0 ;
for i in range ( 31 , - 1 , - 1 ):
if (temp + (divisor << i) < = dividend):
temp + = divisor << i;
quotient | = 1 << i;
if sign = = - 1 :
quotient = - quotient;
return quotient;
a = 10 ;
b = 3 ;
print (divide(a, b));
a = 43 ;
b = - 8 ;
print (divide(a, b));
|
C#
using System;
class GFG
{
public static long divide( long dividend,
long divisor)
{
long sign = ((dividend < 0) ^
(divisor < 0)) ? -1 : 1;
dividend = Math.Abs(dividend);
divisor = Math.Abs(divisor);
long quotient = 0, temp = 0;
for ( int i = 31; i >= 0; --i)
{
if (temp + (divisor << i) <= dividend)
{
temp += divisor << i;
quotient |= 1LL << i;
}
}
if (sign==-1)
quotient=-quotient;
return quotient;
}
public static void Main()
{
int a = 10, b = 3;
Console.WriteLine(divide(a, b));
int a1 = 43, b1 = -8;
Console.WriteLine(divide(a1, b1));
}
}
|
PHP
<?php
function divide( $dividend ,
$divisor )
{
$sign = (( $dividend < 0) ^
( $divisor < 0)) ? -1 : 1;
$dividend = abs ( $dividend );
$divisor = abs ( $divisor );
$quotient = 0;
$temp = 0;
for ( $i = 31; $i >= 0; -- $i )
{
if ( $temp + ( $divisor << $i ) <= $dividend )
{
$temp += $divisor << $i ;
$quotient |= (double)(1) << $i ;
}
}
if ( $sign ==-1)
$quotient =- $quotient ;
return $quotient ;
}
$a = 10;
$b = 3;
echo divide( $a , $b ). "\n" ;
$a = 43;
$b = -8;
echo divide( $a , $b );
?>
|
Javascript
<script>
function divide(dividend, divisor)
{
var sign = ((dividend < 0)?1:0 ^
(divisor < 0)?1:0) ? -1 : 1;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
var quotient = 0, temp = 0;
while (dividend >= divisor) {
dividend -= divisor;
++quotient;
}
if (sign==-1) quotient=-quotient;
return quotient;
}
var a = 10, b = 3;
document.write(divide(a, b) + "<br>" );
var a1 = 43, b1 = -8;
document.write(divide(a1, b1) + "<br>" );
</script>
|
Time complexity : O(log(a))
Auxiliary space : O(1)
Another Efficient Approach : Using the Logarithms
The idea here is to use the following identity:
Basic Idea : a/b = e ln(a) / e ln(b) = e( ln(a) – ln(b) )
C++
#include <bits/stdc++.h>
using namespace std;
long long int divide( long long int dividend, long long int divisor)
{
if (dividend == 0)
return 0;
if (divisor == 0)
{
cout << "Division by 0 is impossible\n" ;
return 0;
}
long long int sign = (dividend < 0) ^ (divisor < 0);
dividend = abs (dividend);
divisor = abs (divisor);
if (divisor == 1)
return ((sign == 0) ? dividend : -dividend);
long long int ans = exp ( log (dividend) - log (divisor)) + 0.0000000001;
return ((sign == 0) ? ans : -ans);
}
int main()
{
int a = 10, b = 3;
cout << divide(a, b) << '\n' ;
a = 41, b = -8;
cout << divide(a, b) << '\n' ;
return 0;
}
|
Java
class GFG {
static long divide( long dividend, long divisor) {
if (dividend == 0 )
return 0 ;
if (divisor == 0 ) {
System.out.println( "Division by 0 is impossible" );
return Integer.MAX_VALUE;
}
boolean sign = (dividend < 0 ) ^ (divisor < 0 );
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if (divisor == 1 )
return ((sign == false ) ? dividend : -dividend);
long ans = ( long ) (Math.exp(Math.log(dividend) - Math.log(divisor)) + 0.0000000001 );
return ((sign == false ) ? ans : -ans);
}
public static void main(String[] args) {
int a = 10 , b = 3 ;
System.out.println(divide(a, b));
a = 41 ;
b = - 8 ;
System.out.println(divide(a, b));
}
}
|
Python3
import math
def divide(dividend, divisor):
if dividend = = 0 :
return 0
if divisor = = 0 :
print ( "Division by 0 is impossible\n" )
return 2 * * 31 - 1
sign = (divisor < 0 ) ^ (dividend < 0 )
dividend = abs (dividend)
divisor = abs (divisor)
if divisor = = 1 :
return dividend if (sign = = 0 ) else - dividend
ans = math.exp(math.log( abs (dividend)) - math.log( abs (divisor)))
return ans if (sign = = 0 ) else - ans
dividend, divisor = 10 , 3
print (divide(dividend, divisor))
dividend, divisor = 41 , - 8
print (divide(dividend, divisor))
|
C#
using System;
public class GFG {
static long divide( long dividend, long divisor)
{
if (dividend == 0)
return 0;
if (divisor == 0) {
Console.WriteLine(
"Division by 0 is impossible" );
return Int32.MaxValue;
}
bool sign = (dividend < 0) ^ (divisor < 0);
dividend = Math.Abs(dividend);
divisor = Math.Abs(divisor);
if (divisor == 1)
return ((sign == false ) ? dividend : -dividend);
long ans = ( long )(Math.Exp(Math.Log(dividend)
- Math.Log(divisor))
+ 0.0000000001);
return ((sign == false ) ? ans : -ans);
}
static public void Main()
{
int a = 10, b = 3;
Console.WriteLine(divide(a, b));
a = 41;
b = -8;
Console.WriteLine(divide(a, b));
}
}
|
Javascript
function divide( dividend, divisor)
{
if (dividend == 0)
return 0;
if (divisor == 0)
{
document.write( "Division by 0 is impossible" );
return 0;
}
let sign = (dividend < 0) ^ (divisor < 0);
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if (divisor == 1)
return ((sign == 0) ? dividend : -dividend);
let ans = Math.exp(Math.log(dividend) - Math.log(divisor)) + 0.0000000001;
ans= Math.round(ans);
return ((sign == 0) ? ans : -ans);
}
let a = 10, b = 3;
console.log( divide(a, b));
a = 41, b = -8;
console.log( divide(a, b));
|
Time Complexity: O(1) as the time taken to perform the division is constant.
Space Complexity: O(1) as no extra space is required.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!