> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Divide Two Integers Python Solution with Tests

> Tested Python solution for LeetCode 29 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 29, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/divide-two-integers/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 29   # by problem number
lcpy gen -s divide_two_integers   # by problem name
```

## Problem

Given two integers `dividend` and `divisor`, divide two integers **without** using multiplication, division, and mod operator.

The integer division should truncate toward zero, which means losing its fractional part. For example, `8.345` would be truncated to `8`, and `-2.7335` would be truncated to `-2`.

Return *the **quotient** after dividing* `dividend` *by* `divisor`.

**Note:** Assume we are dealing with an environment that could only store integers within the **32-bit** signed integer range: `[-2^31, 2^31 - 1]`. For this problem, if the quotient is **strictly greater than** `2^31 - 1`, then return `2^31 - 1`, and if the quotient is **strictly less than** `-2^31`, then return `-2^31`.

### Examples

```
Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333.. which is truncated to 3.
```

```
Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = -2.33333.. which is truncated to -2.
```

### Constraints

* `-2^31 <= dividend, divisor <= 2^31 - 1`
* `divisor != 0`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_two_integers/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_two_integers/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log(n)^2)
    # Space: O(1)
    def divide(self, dividend: int, divisor: int) -> int:
        int_min, int_max = -(2**31), 2**31 - 1
        negative = (dividend < 0) != (divisor < 0)
        magnitude_a = abs(dividend)
        magnitude_b = abs(divisor)

        quotient = 0
        while magnitude_a >= magnitude_b:
            shift = 0
            while magnitude_a >= (magnitude_b << (shift + 1)):
                shift += 1
            quotient += 1 << shift
            magnitude_a -= magnitude_b << shift
        if negative:
            quotient = -quotient
        return max(int_min, min(int_max, quotient))
```

## Complexity

| Time | Space |
| - | - |
| O(log(n)^2) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.