Divide Two Integers LeetCode Solution

Last updated on March 8th, 2025 at 11:54 pm

Here, we see the Divide Two Integers LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Bit-Manipulation, Math

Level of Question

Medium

Divide Two Integers LeetCode Solution

Divide Two Integers LeetCode Solution

1. 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.

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.

2. Coding Pattern Used in Solution

The coding pattern used in the provided code is “Bit Manipulation with Exponential Search”. The code uses bitwise operations (<< for left shift) to perform exponential search, which allows it to efficiently calculate the quotient by doubling the divisor repeatedly until it exceeds the dividend.

3. Code Implementation in Different Languages

3.1 Divide Two Integers C++

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

        long int dd = abs(dividend), dv = abs(divisor);
        int res=0;
        while(dv<=dd) {
            long int sum=dv, count=1;
            while(sum<=dd-sum) {
                sum+=sum;
                count+=count;
            }
            res+=count;
            dd-=sum;
        }
if((dividend<0&&divisor>0) || (dividend>0&&divisor<0)) return -res;
        
        return res;       
    }
};

3.2 Divide Two Integers Java

class Solution {
    public int divide(int dividend, int divisor) {
    boolean isNegative = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ? true : false;
    long absDividend = Math.abs((long) dividend);
    long absDivisor = Math.abs((long) divisor);
    long result = 0;
    while(absDividend >= absDivisor){
        long tmp = absDivisor, count = 1;
        while(tmp <= absDividend){
            tmp <<= 1;
            count <<= 1;
        }
        result += count >> 1;
        absDividend -= tmp >> 1;
    }
    return  isNegative ? (int) ~result + 1 : result > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) result;        
    }
}

3.3 Divide Two Integers JavaScript

var divide = function(dividend, divisor) {
  if (divisor === 0) return 0;
  if (dividend === 0) return 0;
  if (dividend === -2147483648 && divisor === -1) return 2147483647;

  var isPositive = true;
  if (dividend > 0 !== divisor > 0) isPositive = false;

  divisor = Math.abs(divisor);
  dividend = Math.abs(dividend);

  var count = 1,
    result = 0,
    base = divisor;

  while (dividend >= divisor) {
    count = 1;
    base = divisor;
    while (base <= (dividend >> 1)) {
      base = base << 1;
      count = count << 1;
    }
    result += count;
    dividend -= base;
  }

  if (!isPositive) result = -result;
  return result;   
};

3.4 Divide Two Integers Python

class Solution(object):
    def divide(self, dividend, divisor):
        positive = (dividend < 0) is (divisor < 0)
        dividend, divisor = abs(dividend), abs(divisor)
        res = 0
        while dividend >= divisor:
            temp, i = divisor, 1
            while dividend >= temp:
                dividend -= temp
                res += i
                i <<= 1
                temp <<= 1
        if not positive:
            res = -res
        return min(max(-2147483648, res), 2147483647)

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(log²(n))O(1)
JavaO(log²(n))O(1)
JavaScriptO(log²(n))O(1)
PythonO(log²(n))O(1)
  • The code efficiently performs division without using the division operator by leveraging bitwise operations and exponential search.
  • It handles edge cases, works with absolute values, and adjusts the result’s sign at the end.
  • The time complexity is O(log²(N)), and the space complexity is O(1) for all implementations.
Scroll to Top