-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDivideInteger.java
More file actions
120 lines (69 loc) · 1.92 KB
/
DivideInteger.java
File metadata and controls
120 lines (69 loc) · 1.92 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Divide Integers
Problem Description
Divide two integers without using multiplication, division and mod operator.
Return the floor of the result of the division.
Also, consider if there can be overflow cases i.e output is greater than INT_MAX, return INT_MAX.
NOTE: INT_MAX = 2^31 - 1
Problem Constraints
-231 <= A, B <= 231-1
B!= 0
Input Format
First argument is an integer A denoting the dividend.
Second argument is an integer B denoting the divisor.
Output Format
Return an integer denoting the floor value of the division.
Example Input
Input 1:
A = 5
B = 2
Input 2:
A = 7
B = 1
Example Output
Output 1:
2
Output 2:
7
Example Explanation
Explanation 1:
floor(5/2) = 2
*/
/*
Solution Approach
Think in terms of bits.
How do you do the division with bits?
How do you determine the most significant bit in the answer?
Iterate on the bit position ‘i’ from 31 to 1 and find the first bit for which divisor«i is less than dividend.
How do you use (1) to move forward in similar fashion?
*/
public class Solution {
public int divide(int A, int B) {
long n = A, m = B;
// determine sign of the quotient
int sign = 1;
if(n < 0) sign *= -1;
if(m < 0) sign *= -1;
// remove sign of operands
n = Math.abs(n);
m = Math.abs(m);
// q stores the quotient in computation
long q = 0, t = 0;
// test down from the highest bit
// accumulate the tentative value for valid bits
for (int i = 31; i >= 0; i--)
{
if (t + (m << i) <= n)
{ t += m << i;
q |= (long)1 << i;
}
}
// assign back the sign
if (sign < 0) q = -q;
// check for overflow and return
if(q > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
else
return (int)q;
}
}