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: [−231, 231 − 1]
. For this problem, if the quotient is strictly greater than 231 - 1
, then return 231 - 1
, and if the quotient is strictly less than -231
, then return -231
.
Example 1:
Input: dividend = 10, divisor = 3 Output: 3 Explanation: 10/3 = 3.33333.. which is truncated to 3.
Example 2:
Input: dividend = 7, divisor = -3 Output: -2 Explanation: 7/-3 = -2.33333.. which is truncated to -2.
Constraints:
-231 <= dividend, divisor <= 231 - 1
divisor != 0
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to find out how many times one number fits into another. The brute force approach is like repeatedly subtracting the smaller number from the larger number until we can't anymore, then counting how many subtractions we did.
Here's how the algorithm would work step-by-step:
def divide_two_integers(dividend, divisor):
# Handle division by zero.
if divisor == 0:
return float('inf') if dividend > 0 else float('-inf')
# Determine the sign of the result.
negative_result = (dividend < 0) != (divisor < 0)
# Convert both numbers to positive.
positive_dividend = abs(dividend)
positive_divisor = abs(divisor)
quotient = 0
while positive_dividend >= positive_divisor:
# Repeatedly subtract divisor from dividend
positive_dividend -= positive_divisor
quotient += 1
# Adjust the result based on the sign.
if negative_result:
quotient = -quotient
# Handle integer overflow.
max_int = 2**31 - 1
min_int = -2**31
if quotient > max_int:
return max_int
elif quotient < min_int:
return min_int
else:
return quotient
The most efficient way to divide two numbers without using division, multiplication, or mod operations involves repeated subtraction but with a twist. Instead of subtracting the divisor one by one, we strategically subtract larger and larger powers of the divisor, getting closer to the final answer much faster.
Here's how the algorithm would work step-by-step:
def divide_two_integers(dividend, divisor):
# Handle the signs of the dividend and divisor.
sign = -1 if ((dividend < 0) ^ (divisor < 0)) else 1
dividend_abs = abs(dividend)
divisor_abs = abs(divisor)
quotient = 0
# Subtract the divisor until dividend is smaller than divisor.
while dividend_abs >= divisor_abs:
temp_divisor = divisor_abs
multiple = 1
# Find the largest multiple of divisor that is <= dividend.
while dividend_abs >= (temp_divisor << 1):
if temp_divisor > (2**30):
break
temp_divisor <<= 1
multiple <<= 1
dividend_abs -= temp_divisor
quotient += multiple
# Apply the sign to the quotient.
quotient *= sign
# Handle integer overflow.
min_int = -2**31
max_int = 2**31 - 1
if quotient > max_int:
return max_int
elif quotient < min_int:
return min_int
else:
return quotient
Case | How to Handle |
---|---|
Divisor is zero | Handle division by zero by returning Integer.MAX_VALUE or throwing an exception based on problem specifications. |
Dividend is zero | Return zero since any number divided into zero is zero. |
Both dividend and divisor are Integer.MIN_VALUE | Handle this case to avoid potential overflow issues, return 1 based on problem definition if applicable, otherwise, handle appropriately. |
Dividend is Integer.MIN_VALUE and divisor is -1 | This leads to integer overflow, return Integer.MAX_VALUE. |
Dividend is Integer.MAX_VALUE and divisor is 1 | Return Integer.MAX_VALUE as the result. |
Divisor is 1 | Return the dividend as is because division by 1 yields the dividend. |
Divisor is -1 | Return the negation of the dividend, handling the Integer.MIN_VALUE overflow case separately. |
Large dividend and divisor with a very small quotient | The iterative subtraction approach can be slow; use a bit-manipulation approach to optimize for speed. |