/29. Divide Two Integers

29. Divide Two Integers

Medium
Probability19.5% acceptance

Given two signed 32-bit integers, numerator and denominator, compute the integer quotient of numerator divided by denominator without using multiplication, division, or modulo operators. The result should be truncated toward zero. If the quotient exceeds the 32-bit signed integer range [−2^31, 2^31−1], return the respective boundary value. denominator is guaranteed to be non-zero.

Example 1

Input: numerator = 25, denominator = 4

Output: 6

Explanation: 25/4 = 6.25, truncated to 6

Example 2

Input: numerator = -15, denominator = 5

Output: -3

Explanation: -15/5 = -3

Example 3

Input: numerator = 0, denominator = 7

Output: 0

Explanation: 0/7 = 0

Example 4

Input: numerator = 231-1, denominator = 1

Output: 2147483647

Explanation: Result is within bounds

Example 5

Input: numerator = -231, denominator = -1

Output: 2147483647

Explanation: Result exceeds upper bound, so return 2147483647

Constraints

  • -231 <= numerator <= 231 - 1
  • -231 <= denominator <= 231 - 1
  • denominator != 0
  • Result must be truncated toward zero
  • If result > 231 - 1, return 231 - 1
  • If result < -231, return -231
Python (current runtime)

Case 1

Input: numerator = 100, denominator = 7

Expected: 14

Case 2

Input: numerator = -100, denominator = 8

Expected: -12

Case 3

Input: numerator = 12345, denominator = -123

Expected: -100

Case 4

Input: numerator = -2**31, denominator = 1

Expected: -2147483648

Case 5

Input: numerator = 2**31-1, denominator = -1

Expected: -2147483647