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.

Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231.

&nbsp;
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.


&nbsp;
Constraints:


	-231 &lt;= dividend, divisor &lt;= 231 - 1
	divisor != 0


class Solution:
    def divide(self, dividend: int, divisor: int) -> int:
        # Solution 1 - bitwise operator <<   
        """
        positive = (dividend < 0) is (divisor < 0)
        dividend, divisor = abs(dividend), abs(divisor)
		if divisor == 1:
            quotient = dividend
        else:     
			quotient = 0
			while dividend >= divisor:
				temp, i = divisor, 1
				while dividend >= temp:
					dividend -= temp
					quotient += i
					i <<= 1
					temp <<= 1

		if not positive:
			return max(-2147483648, -quotient)
		else:
			return min(quotient, 2147483647)        
        """
        # Solution 2 - cumulative sum - faster than bitwise 
        positive = (dividend < 0) == (divisor < 0)
        dividend, divisor = abs(dividend), abs(divisor)      
        if divisor == 1:
            quotient = dividend
        else:            
            quotient = 0
            _sum = divisor

            while _sum <= dividend:
                current_quotient = 1
                while (_sum + _sum) <= dividend:
                    _sum += _sum
                    current_quotient += current_quotient
                dividend -= _sum
                _sum = divisor
                quotient += current_quotient            
        if not positive:
            return max(-2147483648, -quotient)
        else:
            return min(quotient, 2147483647)    
class Solution {
    public int divide(int dividend, int divisor) {
        if(dividend == 1<<31 && divisor == -1){
            return Integer.MAX_VALUE;
        }
        boolean sign = (dividend >= 0) == (divisor >= 0) ? true : false;
        
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        
        int result = 0;
        while(dividend - divisor >= 0){
            int count = 0;
            while(dividend - (divisor << 1 << count) >= 0){
                count++;
            }
            result += 1 <<count;
            dividend -= divisor << count;
        }
        return sign ? result : -result;
    }
}
class Solution {
public:
    int divide(int dividend, int divisor) {
        if (dividend == INT_MIN && divisor == -1) {
            return INT_MAX;
        }
        long dvd = labs(dividend), dvs = labs(divisor), ans = 0;
        int sign = dividend > 0 ^ divisor > 0 ? -1 : 1;
        while (dvd >= dvs) {
            long temp = dvs, m = 1;
            while (temp << 1 <= dvd) {
                temp <<= 1;
                m <<= 1;
            }
            dvd -= temp;
            ans += m;
        }
        return sign * ans;
    }
};
var divide = function(dividend, divisor) {
    if (dividend == -2147483648 && divisor == -1) return 2147483647;
    let subdividend = Math.abs(dividend)
    let subdivisor = Math.abs(divisor)
    let string_dividend = subdividend.toString()
    let i = 0
    let ans=0
    let remainder = 0
    while(i<string_dividend.length){
        remainder =remainder*10+Number(string_dividend[i])
        let quotient = Math.floor(remainder/subdivisor)
        ans = ans *10 + quotient
        remainder = remainder - quotient*subdivisor
        i++;
    }
    if((dividend >0 && divisor <0) || (dividend <0 && divisor >0)) return 0-ans
    return ans
};
Divide Two Integers