@@ -0,0 +1,1630 @@
+package problems;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/* %0A * Divide two integers without using multiplication, division and mod operator.%0A */%0A%0Apublic class DivideTwoIntegers %7B%0A%0A    public int divide(int dividend, int divisor) %7B%0A        if (divisor == 0)%0A            return dividend %3E 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;%0A        else if (divisor == 1)%0A            return dividend;%0A%0A        boolean sign = true; // positive%0A        long divisorL = divisor;%0A        if (divisor %3C 0) %7B%0A            divisorL = -divisorL;%0A            sign = !sign;%0A        %7D%0A        long dividendL = dividend;%0A        if (dividend %3C 0) %7B%0A            dividendL = -dividendL;%0A            sign = !sign;%0A        %7D%0A%0A        // both are positive%0A        List%3CLong%3E base = new ArrayList%3CLong%3E();%0A        List%3CInteger%3E power = new ArrayList%3CInteger%3E(); // 2%5En%0A        long cur = divisorL;%0A        int power2 = 1;%0A        while (cur %3C= dividendL) %7B%0A            base.add(cur);%0A            cur += cur;%0A            power.add(power2);%0A            power2 += power2;%0A        %7D%0A        %0A        int result = 0;%0A        for (int i = base.size() - 1; i %3E= 0; i--) %7B%0A            long b = base.get(i);%0A            if (dividendL == b) %7B%0A                result += power.get(i);%0A                break;%0A            %7D else if (dividendL %3E b) %7B%0A                dividendL -= b;%0A                result += power.get(i);%0A            %7D            %0A        %7D%0A        return sign ? result : -result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        DivideTwoIntegers di = new DivideTwoIntegers();%0A        System.out.println(di.divide(-1, 1));%0A    %7D%0A%7D%0A

Add Divide Two Integers