Divide two integers without using multiplication, division and mod operator.
public class Solution { public int divide(int dividend, int divisor) { // Start typing your Java solution below // DO NOT write main() function int sign = 1; if(dividend < 0){ sign *= -1; } if(divisor < 0){ sign *= -1; } long a = dividend; long b = divisor; //must cast to long here for dealing with the Integer.MIN_VALUE //because Math.abs(-2147483648) > Integer.MAX_VALUE a = Math.abs(a); b = Math.abs(b); int count = 0; while(a>=b){ long temp = b; int multi = 1; while(a>=temp){ count = multi+1; a = a- temp; temp += temp; multi += multi; } } return sign * count; } }