Divide Two Integers Without Multiplication, Division or Mod

Solve this Problem
Medium25–30 min
Topics
Companies
Practice:LeetCode ↗
Given two integers dividend and divisor, compute their integer division — truncated toward zero — without using the *, /, or % operators. If the true quotient would overflow the 32-bit signed integer range (which only happens when dividing the most negative representable value by -1), clamp the result to the range's maximum instead. Repeatedly subtracting the divisor counts how many times it fits, but one subtraction at a time is as slow as the quotient is large. Doubling the chunk being subtracted — first check if twice the divisor still fits, then four times, then eight — finds the largest power-of-two multiple that fits in one step, cutting the work down to roughly one step per bit of the quotient instead of one step per unit.

Test Case 1:

Input:dividend = 10, divisor = 3
Output:3
Explanation:10 / 3 = 3.33..., truncated toward zero gives 3.

Test Case 2:

Input:dividend = 7, divisor = -3
Output:-2
Explanation:Truncation toward zero, not flooring — -2.33... becomes -2, not -3.

Test Case 3:

Input:dividend = -2147483648, divisor = -1
Output:2147483647
Explanation:The true quotient (2147483648) overflows the 32-bit range, so the result is clamped to the maximum representable value.

Constraints

  • -2³¹ ≤ dividend ≤ 2³¹ − 1
  • -2³¹ ≤ divisor ≤ 2³¹ − 1
  • divisor ≠ 0
  • The result is truncated toward zero; if it would overflow the 32-bit signed range, it is clamped to [-2³¹, 2³¹ − 1]
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Repeated Subtraction

Brute

Subtract the divisor's magnitude from the dividend's magnitude one copy at a time, counting how many subtractions it takes to run out. Correct, but for a large dividend and a small divisor this can mean billions of individual subtractions.

TimeO(dividend / divisor)
SpaceO(1)
1class Solution { 2 public int divide(int dividend, int divisor) { 3 if (dividend == Integer.MIN_VALUE && divisor == -1) { 4 return Integer.MAX_VALUE; 5 } 6 boolean negative = (dividend < 0) != (divisor < 0); 7 long a = Math.abs((long) dividend); 8 long b = Math.abs((long) divisor); 9 long quotient = 0; 10 while (a >= b) { 11 a -= b; 12 quotient++; 13 } 14 return negative ? (int) -quotient : (int) quotient; 15 } 16}

Optimal — Exponential Bit-Shift Search

Optimal

Instead of subtracting the divisor one copy at a time, subtract the largest power-of-two multiple of it that still fits: keep doubling the chunk being subtracted (first check if twice the divisor still fits, then four times, then eight) until doubling again would overshoot. Subtract that chunk in one step, add its matching power of two to the quotient, and repeat on what's left — roughly one step per bit of the quotient instead of one step per unit.

TimeO(log² n)
SpaceO(1)
1class Solution { 2 public int divide(int dividend, int divisor) { 3 if (dividend == Integer.MIN_VALUE && divisor == -1) { 4 return Integer.MAX_VALUE; 5 } 6 boolean negative = (dividend < 0) != (divisor < 0); 7 long a = Math.abs((long) dividend); 8 long b = Math.abs((long) divisor); 9 long quotient = 0; 10 while (a >= b) { 11 long temp = b; 12 long multiple = 1; 13 while (a >= (temp << 1)) { 14 temp <<= 1; 15 multiple <<= 1; 16 } 17 a -= temp; 18 quotient += multiple; 19 } 20 return negative ? (int) -quotient : (int) quotient; 21 } 22}

Related Problems