# Divide Two Integers LeetCode Solution

Last updated on February 22nd, 2024 at 03:30 am

Here, We see Divide Two Integers problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

# List of all LeetCode Solution

## 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.```

## Divide Two Integers Leetcode Solution 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;
}
};
```Code language: C++ (cpp)```

## Divide Two Integers Leetcode Solution 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;
}
}
```Code language: Java (java)```

## Divide Two Integers Leetcode Solution 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;
};
```Code language: JavaScript (javascript)```

## Divide Two Integers Leetcode Solution 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)
```Code language: Python (python)```
Scroll to Top