</>

29. Divide Two Integers

Medium
Bit Manipulation Time O(log(dividend/divisor) Space O(1)
↗ Open on LeetCode Source: Misc

Quick review

Pattern
Bit Manipulation
Time
O(log(dividend/divisor)
Space
O(1)

Standard Solution (C++)

SummaryGiven a dividend and a divisor, compute their integer quotient without using multiplication, division, or mod operators, clamping the result to the 32-bit signed integer range.

Approach
  • Convert both operands to their absolute values (using long long to handle INT_MIN) and track the sign separately.
  • Repeatedly find the largest multiple of the divisor that fits into the remaining dividend by left-shifting the divisor: each shift doubles it, so we home in exponentially fast.
  • Subtract that shifted value and accumulate the corresponding power-of-two into the quotient, then repeat until the remainder is less than the divisor.
  • Handle the one overflow case (INT_MIN / -1) explicitly before anything else, since the true result exceeds INT_MAX.
#include <climits>
#include <cstdlib>
using namespace std;

class Solution {
public:
    int divide(int dividend, int divisor) {
        // Only overflow case: INT_MIN / -1 exceeds INT_MAX
        if (dividend == INT_MIN && divisor == -1) return INT_MAX;

        // Work in non-negative long long to avoid INT_MIN pitfalls
        bool negative = (dividend > 0) ^ (divisor > 0);
        long long a = abs(static_cast<long long>(dividend));
        long long b = abs(static_cast<long long>(divisor));

        long long quotient = 0;
        while (a >= b) {
            // Find largest shift k such that (b << k) <= a
            long long shifted = b;
            long long multiple = 1;
            while ((shifted << 1) <= a) {
                shifted <<= 1;
                multiple <<= 1;
            }
            a -= shifted;
            quotient += multiple;
        }

        return static_cast<int>(negative ? -quotient : quotient);
    }
};
Time O(log^2 n) where n = |dividend|, since the outer loop runs O(log n) times and each inner doubling loop also takes O(log n) steps Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    int divide(int dividend, int divisor) {
        if(dividend==INT_MIN && divisor==-1){
            return INT_MAX;
        }

        bool isNegative=(dividend>0)^(divisor>0);
        long long a=abs((long long)dividend);
        long long b=abs((long long)divisor);
        long long quotient=0;

        while(a>=b){
            long long tmp=b;
            int shift=0;
            while((tmp<<1)<a){
                tmp<<=1;
                ++shift;
            }
            a-=tmp;
            quotient+=(1<<shift);
        }

        return isNegative? -quotient: quotient;
    }
};

// Time Complexity: O(log(dividend/divisor))
// Space Complexity: O(1)
Time O(log(dividend/divisor) Space O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern