[Naive Approach] By Repeated Subtraction - O(a/b) Time and O(1) Space
The basic idea is to first find the sign of quotient. The sign of the quotient will be negative only when the sign of dividend and divisor are different. Now, convert both the dividend and divisor to positive numbers and 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. Finally, we append the sign to the quotient to get the final result.
This approach fails for large values of quotient.
C++
// C++ Program to divide two integers by repeated subtraction#include<bits/stdc++.h>usingnamespacestd;// Function to divide a by b and// return floor value of the resultlonglongdivide(longlonga,longlongb){if(a==0)return0;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// Convert divisor and dividend to positive numbersa=abs(a);b=abs(b);// Initialize the quotientlonglongquotient=0;// Perform repeated subtraction till dividend// is greater than divisorwhile(a>=b){a-=b;++quotient;}returnquotient*sign;}intmain(){longlonga=43,b=-8;cout<<divide(a,b)<<"\n";return0;}
C
// C Program to divide two integers by repeated subtraction#include<stdio.h>#include<stdlib.h>// Function to divide a by b and// return floor value of the resultlonglongdivide(longlonga,longlongb){if(a==0)return0;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// Convert divisor and dividend to positive numbersa=abs(a);b=abs(b);// Initialize the quotientlonglongquotient=0;// Perform repeated subtraction till dividend// is greater than divisorwhile(a>=b){a-=b;++quotient;}returnquotient*sign;}intmain(){longlonga=43,b=-8;printf("%lld\n",divide(a,b));return0;}
Java
// Java Program to divide two integers by repeated subtractionclassGfG{// Function to divide a by b and// return floor value of the resultstaticlongdivide(longa,longb){if(a==0)return0;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// Convert divisor and dividend to positive numbersa=Math.abs(a);b=Math.abs(b);// Initialize the quotientlongquotient=0;// Perform repeated subtraction till dividend// is greater than divisorwhile(a>=b){a-=b;++quotient;}returnquotient*sign;}publicstaticvoidmain(String[]args){longa=43,b=-8;System.out.println(divide(a,b));}}
Python
# Python Program to divide two integers by repeated subtraction# Function to divide a by b and# return floor value of the resultdefdivide(a,b):ifa==0:return0# The sign will be negative only if sign of # divisor and dividend are differentsign=-1if(a<0)^(b<0)else1# Convert divisor and dividend to positive numbersa=abs(a)b=abs(b)# Initialize the quotientquotient=0# Perform repeated subtraction till dividend# is greater than divisorwhilea>=b:a-=bquotient+=1returnquotient*signif__name__=="__main__":a=43b=-8print(divide(a,b))
C#
// C# Program to divide two integers by repeated subtractionusingSystem;classGfG{// Function to divide a by b and// return floor value of the resultstaticlongdivide(longa,longb){if(a==0)return0;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// Convert divisor and dividend to positive numbersa=Math.Abs(a);b=Math.Abs(b);// Initialize the quotientlongquotient=0;// Perform repeated subtraction till dividend// is greater than divisorwhile(a>=b){a-=b;++quotient;}returnquotient*sign;}staticvoidMain(string[]args){longa=43,b=-8;Console.WriteLine(divide(a,b));}}
JavaScript
// JavaScript Program to divide two integers by repeated subtraction// Function to divide a by b and// return floor value of the resultfunctiondivide(a,b){if(a===0)return0;// The sign will be negative only if sign of // divisor and dividend are differentconstsign=((a<0)^(b<0))?-1:1;// Convert divisor and dividend to positive numbersa=Math.abs(a);b=Math.abs(b);// Initialize the quotientletquotient=0;// Perform repeated subtraction till dividend// is greater than divisorwhile(a>=b){a-=b;++quotient;}returnquotient*sign;}consta=43,b=-8;console.log(divide(a,b));
Output
-5
Time complexity: O(a/b), where a is the dividend and b is the divisor. Auxiliary space: O(1)
[Expected Approach] Using Bit Manipulation - O(log(a)) Time and O(1) Space
The idea is similar to the previous approach with the only difference that instead of subtracting one unit of divisor from the dividend, we can subtract multiple units of divisor at once until the dividend becomes smaller than divisor.
Division without using multiplication and division operator
This can be easily done by iterating on the bit position i = 30 to 0. For each bit position i, where (divisor << i) is less than dividend, set the ith bit of the quotient and subtract (divisor << i) from the dividend. After iterating over all the bits, return the quotient with the corresponding sign.
C++
// C++ implementation to Divide two// integers using Bit Manipulation#include<iostream>#include<limits.h>usingnamespacestd;// Function to divide a by b and// return floor value itlonglongdivide(longlonga,longlongb){// Handle overflowif(a==INT_MIN&&b==-1)returnINT_MAX;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// remove sign of operandsa=abs(a);b=abs(b);// Initialize the quotientlonglongquotient=0;// Iterate from most significant bit to // least significant bitfor(inti=31;i>=0;--i){// Check if (divisor << i) <= dividendif((b<<i)<=a){a-=(b<<i);quotient|=(1LL<<i);}}returnsign*quotient;}intmain(){longlonga=43,b=-8;cout<<divide(a,b);return0;}
C
// C implementation to Divide two// integers using Bit Manipulation#include<stdio.h>#include<limits.h>// Function to divide a by b and// return floor value itlonglongdivide(longlonga,longlongb){// Handle overflowif(a==INT_MIN&&b==-1)returnINT_MAX;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// remove sign of operandsa=llabs(a);b=llabs(b);// Initialize the quotientlonglongquotient=0;// Iterate from most significant bit to // least significant bitfor(inti=31;i>=0;--i){// Check if (divisor << i) <= dividendif((b<<i)<=a){a-=(b<<i);quotient|=(1LL<<i);}}returnsign*quotient;}intmain(){longlonga=43,b=-8;printf("%lld\n",divide(a,b));return0;}
Java
// Java implementation to Divide two// integers using Bit ManipulationclassGfG{// Function to divide a by b and// return floor value itstaticlongdivide(longa,longb){// Handle overflowif(a==Integer.MIN_VALUE&&b==-1)returnInteger.MAX_VALUE;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// remove sign of operandsa=Math.abs(a);b=Math.abs(b);// Initialize the quotientlongquotient=0;// Iterate from most significant bit to // least significant bitfor(inti=31;i>=0;--i){// Check if (divisor << i) <= dividendif((b<<i)<=a){a-=(b<<i);quotient|=(1L<<i);}}returnsign*quotient;}publicstaticvoidmain(String[]args){longa=43,b=-8;System.out.println(divide(a,b));}}
Python
# Python implementation to Divide two# integers using Bit Manipulation# Function to divide a by b and# return floor value itdefdivide(a,b):# Handle overflowifa==-2**31andb==-1:return2**31-1# The sign will be negative only if sign of # divisor and dividend are differentsign=-1if(a<0)^(b<0)else1# remove sign of operandsa=abs(a)b=abs(b)# Initialize the quotientquotient=0# Iterate from most significant bit to # least significant bitforiinrange(31,-1,-1):# Check if (divisor << i) <= dividendif(b<<i)<=a:a-=(b<<i)quotient|=(1<<i)returnsign*quotientif__name__=="__main__":a=43b=-8print(divide(a,b))
C#
// C# implementation to Divide two// integers using Bit ManipulationusingSystem;classGfG{// Function to divide a by b and// return floor value itstaticlongdivide(longa,longb){// Handle overflowif(a==int.MinValue&&b==-1)returnint.MaxValue;// The sign will be negative only if sign of // divisor and dividend are differentintsign=((a<0)^(b<0))?-1:1;// remove sign of operandsa=Math.Abs(a);b=Math.Abs(b);// Initialize the quotientlongquotient=0;// Iterate from most significant bit to // least significant bitfor(inti=31;i>=0;--i){// Check if (divisor << i) <= dividendif((b<<i)<=a){a-=(b<<i);quotient|=(1L<<i);}}returnsign*quotient;}staticvoidMain(){longa=43,b=-8;Console.WriteLine(divide(a,b));}}
Output
-5
Time Complexity: O(log(a)), as we are iterating over all the bits of a. Auxiliary Space: O(1)