DEV Community

Varit Patel
Varit Patel

Posted on

LeetCode Question 29

29. Divide Two Integers

Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.

Let's solve it :-)

var divide = function(dividend, divisor) {
};
Enter fullscreen mode Exit fullscreen mode

Function divide has two parameters dividend and divisor.

First of all, Let's check some weird scenarios:

  1. Question has a note which says 32-bit signed integer range: [−2*31, 2*31 − 1] so this indicates the max and min(on Negative side) number which we need to process
  2. What if the dividend and divisor both have negative value
  3. What if the dividend or divisor has negative value

Let's code it ;-)

 const signedIntegerValue = 2**31
 var divide = function(dividend, divisor) {
    const retIsNegative = Math.sign(divisor) !== Math.sign(dividend);
    dividend = Math.abs(dividend)
    divisor = Math.abs(divisor)

    let result = 0

    // Logic here below...

    return retIsNegative ? -result : result
};
Enter fullscreen mode Exit fullscreen mode

Define the 32 bit signed integer range as constant at above

Now inside function calculate weather we have one of the value as negative or not.

If both signs are not same then, return value is negative.

Next lines, Makes both of them to the absolute value as it will be easy to handle the sign later.

And predefined result value 0 and added result value as the return value for the function.

_Logic _

    while (divisor <= dividend) {
        let value = divisor
        let multiple = 1
        while (value + value <= dividend) {
            value += value
            multiple += multiple
        }
        dividend = dividend - value
        result += multiple
    }

    if (result > (signedIntegerValue - 1)) {
        return retIsNegative ? -signedIntegerValue : signedIntegerValue - 1
    }
Enter fullscreen mode Exit fullscreen mode

Consider dividend value is 10 and divisor value is 3.
Running a while loop till the divisor is less than dividend.

Assigning divisor value to value variable

Initially, considering multiple value is 1.

Running a nest while loop when (*value + value *) which is 2 * divisor value is less than dividend - This loop runs for the logarithmic purpose to remove the values from dividend

so now the value = value + value which is (3 + 3) = 6

and multiple value is now 2

Now, nested while loop check the condition

so new value of the value = value + value which is (6 + 3) = 9 and 9 < divisor 10

New value of value is 9 and multiple stands as 3

Now, nested while loop check the condition, where 12 <= 10 which is false.

Hence nested loop is over and next line after the loop starts executing.

Hence new value of dividend is 10 - 9 = 1

return value is result(0) + multiple(3) = 3

Checking the return value with the signed 32 integer value

and returning according to the signatures for the max and min signed 32 integer value.

If the return is not greater than signed integer value then it returns as the signed value suggests.

Full Code as follows:

const signedIntegerValue = 2 ** 31
var divide = function (dividend, divisor) {
    const retIsNegative = Math.sign(divisor) !== Math.sign(dividend);
    dividend = Math.abs(dividend)
    divisor = Math.abs(divisor)

    let result = 0
    while (divisor <= dividend) {
        let value = divisor
        let multiple = 1
        while (value + value <= dividend) {
            value += value
            multiple += multiple
        }
        dividend = dividend - value
        result += multiple
    }

    if (result > (signedIntegerValue - 1)) {
        return retIsNegative ? -signedIntegerValue : signedIntegerValue - 1
    }
    return retIsNegative ? -result : result
};
Enter fullscreen mode Exit fullscreen mode

Hope this helps. Happy Coding!

Top comments (0)