-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDivideTwoIntegers.java
More file actions
18 lines (15 loc) · 897 Bytes
/
DivideTwoIntegers.java
File metadata and controls
18 lines (15 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int divide(int dividend, int divisor) {
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; //Cornor case when -2^31 is divided by -1 will give 2^31 which doesnt exist so overflow
boolean negative = dividend < 0 ^ divisor < 0; //Logical XOR will help in deciding if the results is negative only if any one of them is negative
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
int quotient = 0, subQuot = 0;
while (dividend - divisor >= 0) {
for (subQuot = 0; dividend - (divisor << subQuot << 1) >= 0; subQuot++);
quotient += 1 << subQuot; //Add to the quotient
dividend -= divisor << subQuot; //Substract from dividend to start over with the remaining
}
return negative ? -quotient : quotient;
}
}