-
Notifications
You must be signed in to change notification settings - Fork 688
/
IntegerDivision.java
61 lines (49 loc) · 1.36 KB
/
IntegerDivision.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package math;
public class IntegerDivision {
/*
* Divide two integers without using '/' (division) or '*' (multiplication) operators.
* Eg: 7 ÷ 2 = 3
*
* Runtime Complexity:
* Logarithmic, O(logn).
*
* Memory Complexity:
* Logarithmic, O(logn)
*
*
* */
public static int integerDivide(int x, int y) {
// We will return -1 if the
// divisor is '0'.
if (y == 0) {
return -1;
}
if (x < y) {
return 0;
} else if (x == y) {
return 1;
} else if (y == 1) {
return x;
}
int q = 1;
int val = y;
while (val < x) {
val <<= 1;
// we can also use 'val = val + val;'
q <<= 1;
// we can also use 'q = q + q;'
}
if (val > x) {
val >>= 1;
q >>= 1;
return q + integerDivide(x - val, y);
}
return q;
}
public static void main(String[] args) {
System.out.println("5/11 " + String.valueOf(integerDivide(55, 11) == 5));
System.out.println("54/2 " + String.valueOf(integerDivide(54, 2) == 27));
System.out.println("51/13 " + String.valueOf(integerDivide(51, 13) == 3));
System.out.println("51/0 " + String.valueOf(integerDivide(51, 0) == -1));
}
}