From ecf36e2993cbee39de78b3fb41b7f5f604a12bff Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Fri, 24 Oct 2025 13:34:51 +0530 Subject: [PATCH 1/4] Add algorithm: divide two integers in bit_manipulation --- bit_manipulation/divide_two_integers.py | 76 +++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 bit_manipulation/divide_two_integers.py diff --git a/bit_manipulation/divide_two_integers.py b/bit_manipulation/divide_two_integers.py new file mode 100644 index 000000000000..41d588a4ee52 --- /dev/null +++ b/bit_manipulation/divide_two_integers.py @@ -0,0 +1,76 @@ +class Solution: + def divide(self, dividend: int, divisor: int) -> int: + """ + Divide two integers without using *, /, or % operators. + + LeetCode Problem: https://leetcode.com/problems/divide-two-integers/ + + Args: + dividend (int): The number to be divided. + divisor (int): The number to divide by. + + Returns: + int: The quotient after truncating toward zero. + + Examples: + >>> sol = Solution() + >>> sol.divide(43, 8) + 5 + >>> sol.divide(10, 3) + 3 + >>> sol.divide(-7, 2) + -3 + >>> sol.divide(1, 1) + 1 + >>> sol.divide(-2147483648, -1) # overflow case + 2147483647 + >>> sol.divide(24, 8) + 3 + >>> sol.divide(43, -8) + -5 + """ + # Edge case: if both are equal, result is 1 + if dividend == divisor: + return 1 + + # Determine the sign of the result + sign = not ( + (dividend < 0) ^ (divisor < 0) + ) # True if same sign, False if different + + # Convert both numbers to positive + d = abs(dividend) # remaining dividend + n = abs(divisor) # divisor + quo = 0 # quotient + + # Outer loop: subtract divisor multiples from dividend + while d >= n: + cnt = 0 + + # Inner loop: find largest power-of-two multiple of divisor that fits in dividend + while d >= (n << (cnt + 1)): + cnt += 1 + + # Add this power-of-two chunk to quotient + quo += 1 << cnt + + # Subtract the chunk from remaining dividend + d -= n << cnt + + # Handle overflow for 32-bit signed integers + if quo == (1 << 31): + return (2**31 - 1) if sign else -(2**31) + + return quo if sign else -quo + + +if __name__ == "__main__": + import doctest + + doctest.testmod() # Run the doctest examples + + # Optional: interactive test + sol = Solution() + dividend = int(input("Enter dividend: ")) + divisor = int(input("Enter divisor: ")) + print(f"Quotient: {sol.divide(dividend, divisor)}") From d917069ba3713faffad404376f177f0633ce8672 Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Fri, 24 Oct 2025 13:43:58 +0530 Subject: [PATCH 2/4] Fix issues and improve divide_two_integers algorithm --- bit_manipulation/divide_two_integers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/divide_two_integers.py b/bit_manipulation/divide_two_integers.py index 41d588a4ee52..08af5afad94d 100644 --- a/bit_manipulation/divide_two_integers.py +++ b/bit_manipulation/divide_two_integers.py @@ -47,7 +47,8 @@ def divide(self, dividend: int, divisor: int) -> int: while d >= n: cnt = 0 - # Inner loop: find largest power-of-two multiple of divisor that fits in dividend + # Inner loop: find largest power-of-two multiple of divisor + # that fits in dividend while d >= (n << (cnt + 1)): cnt += 1 From b5b9df82adc444974a57b8ee549f46909cfce835 Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Fri, 24 Oct 2025 13:47:47 +0530 Subject: [PATCH 3/4] Fix trailing whitespace in divide_two_integers.py --- bit_manipulation/divide_two_integers.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/divide_two_integers.py b/bit_manipulation/divide_two_integers.py index 08af5afad94d..e1d553f2995b 100644 --- a/bit_manipulation/divide_two_integers.py +++ b/bit_manipulation/divide_two_integers.py @@ -46,9 +46,8 @@ def divide(self, dividend: int, divisor: int) -> int: # Outer loop: subtract divisor multiples from dividend while d >= n: cnt = 0 - - # Inner loop: find largest power-of-two multiple of divisor - # that fits in dividend + """Inner loop: find largest power-of-two multiple of divisor + that fits in dividend """ while d >= (n << (cnt + 1)): cnt += 1 From ef8f4b2776f9565e69cc8b4f9db5513693907a05 Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Fri, 24 Oct 2025 14:14:39 +0530 Subject: [PATCH 4/4] Fix divide integers problem with bit manipulation + doctests --- bit_manipulation/divide_two_integers.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/bit_manipulation/divide_two_integers.py b/bit_manipulation/divide_two_integers.py index e1d553f2995b..354ffcbba87f 100644 --- a/bit_manipulation/divide_two_integers.py +++ b/bit_manipulation/divide_two_integers.py @@ -22,21 +22,19 @@ def divide(self, dividend: int, divisor: int) -> int: -3 >>> sol.divide(1, 1) 1 - >>> sol.divide(-2147483648, -1) # overflow case + >>> sol.divide(-2147483648, -1) 2147483647 >>> sol.divide(24, 8) 3 >>> sol.divide(43, -8) -5 """ - # Edge case: if both are equal, result is 1 + # Edge case: if dividend equals divisor, result is 1 if dividend == divisor: return 1 # Determine the sign of the result - sign = not ( - (dividend < 0) ^ (divisor < 0) - ) # True if same sign, False if different + sign = not ((dividend < 0) ^ (divisor < 0)) # True if same sign, # Convert both numbers to positive d = abs(dividend) # remaining dividend @@ -46,8 +44,7 @@ def divide(self, dividend: int, divisor: int) -> int: # Outer loop: subtract divisor multiples from dividend while d >= n: cnt = 0 - """Inner loop: find largest power-of-two multiple of divisor - that fits in dividend """ + # find largest power-of-two multiple of divisor that fits in dividend while d >= (n << (cnt + 1)): cnt += 1 @@ -67,10 +64,4 @@ def divide(self, dividend: int, divisor: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() # Run the doctest examples - - # Optional: interactive test - sol = Solution() - dividend = int(input("Enter dividend: ")) - divisor = int(input("Enter divisor: ")) - print(f"Quotient: {sol.divide(dividend, divisor)}") + doctest.testmod()