Last active
September 9, 2021 16:20
-
-
Save ssshukla26/28fe3b099d672876769b6a59e3377d91 to your computer and use it in GitHub Desktop.
Division without using division, multiplication or mod operator [Leetcode 29]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def divide(self, dividend: int, divisor: int) -> int: | |
| # -2147483648 through 2147483647 | |
| MIN_VAL = -2147483648 | |
| MAX_VAL = 2147483647 | |
| # If divisor is 1 or -1 | |
| if abs(divisor) == 1: | |
| if dividend == MIN_VAL and divisor == -1: | |
| return MAX_VAL | |
| else: | |
| return dividend if divisor > 0 else -dividend | |
| # Get if the answer will be negative or positive | |
| sign = -1 if (dividend < 0 and divisor > 0) or (divisor < 0 and dividend > 0) else 1 | |
| res = 0 # result | |
| shift = 31 # At max 31 left shift can be done on a integer value | |
| dividend = abs(dividend) # always take absolute value | |
| divisor = abs(divisor) # always take absolute value | |
| # While dividend is greater than divisor | |
| while dividend >= divisor: | |
| # Check how many shifts require of divisor | |
| # to reach dividend in whole | |
| while dividend < (divisor << shift): | |
| shift -= 1 | |
| # subtract the left shifted divisor from dividend | |
| # and do the same process for the remaining value | |
| dividend = dividend - (divisor<<shift) | |
| # Answer will be how many total left shifted value of 1 | |
| res = res + (1<<shift) | |
| # return result within range and with proper sign | |
| res = min(MAX_VAL, max(MIN_VAL, res)) | |
| # return | |
| return res if sign > 0 else -res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment