Opening the reading…
Opening the reading…
BIT MANIPULATION › BASIC BIT CONCEPTS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A quotient counts how many copies of the divisor fit inside the dividend. Subtracting one copy at a time gives the right answer, but it can require billions of iterations. Instead, keep doubling the divisor: b, 2b, 4b, 8b, and so on. The largest doubled copy that still fits contributes that power of two to the quotient.
After removing that largest chunk, repeat with the remainder. This is binary long division: every selected chunk is a distinct power-of-two multiple of the divisor, and the selected powers add to the quotient. Work with non-negative long long magnitudes so INT_MIN can be represented safely, determine the sign separately, and restore it only after the magnitude is complete.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(w^2) | The outer loop selects at most O(w) power-of-two chunks. For each selected chunk, the inner loop can double through at most O(w) bit positions, so the total work is O(w^2), which is constant for the fixed 32-bit range. |
| Space | O(1) extra | Only a fixed number of scalar variables are used, and the required integer result is not working memory. The bound stays constant even for the worst input shape because no array, recursion stack, or result-sized buffer is created. |
#include <climits>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == INT_MIN && divisor == -1) {
return INT_MAX;
}
bool negative = (dividend < 0) != (divisor < 0);
long long remaining = dividend < 0
? -(long long)dividend
: (long long)dividend;
long long divisorMagnitude = divisor < 0
? -(long long)divisor
: (long long)divisor;
long long quotient = 0;
while (remaining >= divisorMagnitude) {
long long chunk = divisorMagnitude;
long long chunkMultiple = 1;
while (remaining >= (chunk << 1)) {
chunk <<= 1;
chunkMultiple <<= 1;
}
remaining -= chunk;
quotient += chunkMultiple;
}
if (negative) {
quotient = -quotient;
}
if (quotient > INT_MAX) {
return INT_MAX;
}
if (quotient < INT_MIN) {
return INT_MIN;
}
return (int)quotient;
}
};The inner loop maintains two linked quantities: chunk is a power-of-two multiple of the divisor, and chunkMultiple records which multiple it is. They must be doubled together. Once the next doubling would exceed the remaining magnitude, subtract the current chunk and add its recorded multiple; restarting from the original divisor lets the next pass choose the largest chunk that fits the new remainder.
The conversion to long long happens before negation. That ordering matters for dividend equal to INT_MIN, whose positive magnitude is 2^31 and cannot be stored in an int. The explicit INT_MIN divided by -1 check is still required because even a safe long long calculation produces a quotient that cannot be returned as a signed 32-bit integer.