Bitwise Quotient Without Multiply, Divide, or Modulo
Math & Bit ManipulationAuto-graded · 25 test casesLast updated
Compute a truncated 32-bit signed integer quotient using only shifts, addition and subtraction, handling the single overflow case and sign rules efficiently.
Quotient Without Arithmetic Division
You are given two integers, numerator and denominator, both within the range of a 32-bit signed integer. Compute the integer quotient of numerator divided by denominator.
You may not use the multiplication operator *, the division operators / or //, or the modulo operator %. Shifts, addition, subtraction, comparisons and negation are all allowed.
Rules
- The quotient is truncated toward zero. For example,
7 / -3gives-2, and-7 / 3gives-2(not-3). denominatoris never zero.- If the true quotient falls outside the signed 32-bit range, return
2147483647(that is,2^31 - 1). Note that a result of exactly-2^31is within range and must be returned unchanged. - Your solution should be much faster than removing the denominator from the numerator one copy at a time. A running time proportional to the number of bits is expected.
Task
Implement int_quotient(numerator, denominator) that returns the truncated quotient as an integer, following the rules above.
Constraints
-2^31 <= numerator <= 2^31 - 1-2^31 <= denominator <= 2^31 - 1denominator != 0- The operators
*,/,//and%must not be used (and neither shoulddivmod,pow-based tricks, or float division).
Examples
Example 1
Input: int_quotient(10, 3)
Output: 3
Example 2
Input: int_quotient(7, -3)
Output: -2
Example 3
Input: int_quotient(-7, 3)
Output: -2
Example 4
Input: int_quotient(-2147483648, -1)
Output: 2147483647
Hints
Hint 1
Removing the denominator one copy at a time is far too slow for inputs like -2^31 divided by 1. Think about removing big chunks: a denominator shifted left by k bits equals denominator times 2^k.
Hint 2
Work with absolute values and apply the sign at the end. Then, for each bit position from high to low, check whether the shifted denominator still fits in what remains of the numerator; if so, subtract it and record that bit in the answer.
Hint 3
Only one input pair can overflow: -2^31 divided by -1. Handle it before anything else, and be careful not to clamp -2^31 divided by 1, which is a valid result.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Division asks how many copies of the denominator fit into the numerator. Subtracting one copy at a time needs up to about 2 billion steps, which is too slow. Instead, use binary long division: subtract large power-of-two multiples of the denominator, produced with bit shifts.
- Overflow case. The only pair whose true quotient does not fit in 32 bits is
-2^31 / -1 = 2^31. Return2^31 - 1immediately. - Sign. The result is negative exactly when the operands have different signs. Work with absolute values and apply the sign at the end. Python integers are unbounded, so
abs(-2^31)is safe. - Bit loop. For
shiftfrom 31 down to 0, check whetherunit * 2^shiftfits inremaining. If it does, subtract it and add2^shiftto the quotient. The check is written as(remaining >> shift) >= unit, which is equivalent and avoids ever forming a shifted denominator that might overflow in fixed-width languages. - Apply the sign by negating the quotient if needed.
Why it is correct
Let A = |numerator| and b = |denominator|. Throughout the loop, A = quotient * b + remaining, and after processing bit k, remaining < b * 2^k. Initially A <= 2^31 < b * 2^32, so the bound holds before the first step. Because remaining < b * 2^(k+1) at the start of step k, the multiple b * 2^k can be subtracted at most once, so each quotient bit is 0 or 1. After bit 0, 0 <= remaining < b, so quotient = floor(A / b).
For magnitudes, floor and truncation agree, so applying the sign afterward truncates toward zero for negative results (-7 / 3 gives -2).
The quotient magnitude is at most 2^31, and it equals 2^31 only for -2^31 / ±1. With -1 that is the overflow case handled first; with 1 the result is -2^31, which is in range and returned as is.
Complexity
- Time: O(32), which is constant for 32-bit inputs (O(log |numerator|) in general).
- Space: O(1).
Reference solution
def int_quotient(numerator: int, denominator: int) -> int:
INT_MIN = -(1 << 31)
INT_MAX = (1 << 31) - 1
# The only input pair whose true result exceeds the 32-bit range.
if numerator == INT_MIN and denominator == -1:
return INT_MAX
negative = (numerator < 0) != (denominator < 0)
remaining = abs(numerator)
unit = abs(denominator)
quotient = 0
for shift in range(31, -1, -1):
# remaining >> shift >= unit <=> remaining >= unit * 2^shift
if (remaining >> shift) >= unit:
remaining -= unit << shift
quotient += 1 << shift
return -quotient if negative else quotient