GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Divide two integers without using multiplication, division and mod operator.

Divide two integers without using multiplication, division and mod operator.

Return the floor of the result of the division.

Example:

5 / 2 = 2

Also, consider if there can be overflow cases. For overflow case, return INT_MAX.

asked Oct 4, 2015 by Swanky 1 flag

5 Answers

Think in terms of bits.

How do you do the division with bits?

  1. How do you determine the most significant bit in the answer?

Iterate on the bit position ‘i’ from 31 to 1 and find the first bit for which divisor « i is less than dividend.

  1. How do you use (1) to move forward in similar fashion?
#include<bits/stdc++.h>
using namespace std;
        
int divide(int dividend, int divisor) {
    long long n = dividend, m = divisor;
    // determine sign of the quotient
    int sign = n < 0 ^ m < 0 ? -1 : 1;

    // remove sign of operands
    n = abs(n), m = abs(m);

    // q stores the quotient in computation
    long long q = 0;

    // test down from the highest bit
    // accumulate the tentative value for valid bits
    for (long long t = 0, i = 31; i >= 0; i--)
        if (t + (m << i) <= n)
            t += m << i, q |= 1LL << i;

    // assign back the sign
    if (sign < 0) q = -q;

    // check for overflow and return
    return q >= INT_MAX || q < INT_MIN ? INT_MAX : q;
}    

int main() {
	
	cout << divide(-4, 2) << endl;
	return 0;	
}

 

answered Oct 4, 2015 by Gaurav

hey can ypu please explain this 

if (t + (m << i) <= n)

            t += m << i, q |= 1LL << i;

//you can modify this also

#include <stdio.h>
int divide(int nu,int de)
{
    
    int count = 0;
    while(nu>0)
    {
        nu= nu-de;
        count++;
    }
    if(nu<0)
    return count-1;
    else
    return count;
}

int main()
{
    int n;
n=divide(5,2);
printf("%d",n);

    return 0;
}

answered Nov 14, 2016 by Manish Gaur

your test case fails at 

nu=1 de=-1

This is a simple task. Keep subtracting the divisor from dividend until dividend becomes less than divisor. Then divisor will be the remainder at the end and the number of times subtraction is done is actually the quotient. See the simplest implementation below. Thank you

#include <iostream>

using namespace std;

int main()
{
    int quotient=0;
    int dividend = 100;
    int divisor = 10;
    while(dividend>=divisor)
    {
        dividend -=divisor;
        quotient++;
    }
    cout<<"Quotient = "<<quotient<<"\nRemainder = "<<dividend<<endl;
    return 0;
}

Hope it helped. Up vote it if you are satisfied.

answered Nov 14, 2016 by Mazhar MIK

your test case fails at 

nu=1 de=-1

private int helper(int i){
        
        if((i&1)==0){
            i>>=1;
        }else{
            i--;
            i>>=1;
        }
        return i;
    }

answered Jan 30, 2017 by 54520zhujq

How to prove that this always give the answer?

I mean how can one thing that this is the way to solve it?

answered Jul 2, 2016 by sourabhmnnit
...