Divide Two Integers - Complete Solution Guide
Divide Two Integers is LeetCode problem 29, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given two integers dividend and divisor , divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8 , and -2.7335 would be truncated to -2 . Return the quotient after dividing dividend by divisor . Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2 31 , 2 31 − 1]
Detailed Explanation
The problem asks us to implement integer division without using multiplication, division, or the modulo operator. Given two integers, `dividend` and `divisor`, we need to find the quotient of their division, truncating towards zero. The solution must also handle the 32-bit signed integer range constraint. If the quotient exceeds the maximum value (2<sup>31</sup> - 1) or falls below the minimum value (-2<sup>31</sup>), we should return the respective limit.
Solution Approach
The provided solution employs a bit manipulation technique to simulate the division process. It iteratively subtracts multiples of the divisor (shifted by powers of 2) from the dividend, effectively determining how many times the divisor fits into the dividend. The algorithm first handles the edge case where dividing the minimum integer by -1 would result in integer overflow. It then determines the sign of the quotient. After that, both the dividend and divisor are converted to their absolute values to simplify the iterative subtraction. The core of the algorithm lies in iterating through the bits from the most significant bit (31st bit) down to the least significant bit. In each iteration, it checks if the divisor, shifted left by `i` bits (effectively multiplying it by 2<sup>i</sup>), is less than or equal to the dividend. If it is, it means that 2<sup>i</sup> multiples of the divisor can be subtracted from the dividend. The quotient is then updated by adding 2<sup>i</sup>, and the dividend is reduced by the shifted divisor. Finally, the solution returns the quotient multiplied by the correct sign, handling potential overflow issues at the end (in the C++ solution).
Step-by-Step Algorithm
- Step 1: Handle the overflow edge case: If the dividend is INT_MIN and the divisor is -1, return INT_MAX.
- Step 2: Determine the sign of the quotient. If the signs of the dividend and divisor are different, the sign is negative; otherwise, it's positive.
- Step 3: Convert both dividend and divisor to their absolute values to simplify the subtraction process. This might involve casting to a `long` to prevent overflow of INT_MIN.
- Step 4: Initialize the quotient to 0.
- Step 5: Iterate from i = 31 down to 0. In each iteration:
- Step 6: Check if the divisor shifted left by `i` bits (divisor << i) is less than or equal to the dividend.
- Step 7: If it is, update the quotient by adding 2<sup>i</sup> (quotient |= (1 << i)) and reduce the dividend by divisor << i (dividend -= divisor << i).
- Step 8: After the loop finishes, return the quotient multiplied by the sign.
- Step 9: (In C++) perform a check to ensure that `sign * quotient` does not cause an overflow, and return INT_MAX or INT_MIN if it does.
Key Insights
- Insight 1: Bit manipulation can be used to simulate division. Specifically, left bit shifting (<<) is equivalent to multiplying by powers of 2, and right bit shifting (>>) is equivalent to dividing by powers of 2.
- Insight 2: Repeated subtraction can be done efficiently by subtracting increasingly larger multiples of the divisor (powers of 2) from the dividend until the dividend becomes smaller than the divisor.
- Insight 3: Handling edge cases like division by zero (which is prohibited by the problem constraints, but good practice to acknowledge) and integer overflow is crucial for a robust solution. The most significant edge case is when dividend is INT_MIN and the divisor is -1, resulting in INT_MAX + 1.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: Math, Bit Manipulation.
Companies
Asked at: Adobe, Amazon, Apple, Bloomberg, Meta, Microsoft, Nextdoor, TikTok, Uber, Yahoo, Zepto.