470. Divide Two Integers Without Division (BS Approach)

EasyBinary SearchMathBinary Search

Given two integers dividend and divisor, divide them without using multiplication, division, or mod operators. Return the integer quotient truncated toward zero. Clamp result to [-2^31, 2^31-1] if overflow.

Input: Integer dividend and integer divisor (divisor != 0).

Output: Integer quotient truncated toward zero, clamped to 32-bit int range.

Examples

Example 1
Input: 10, 3
Output: 3
Explanation: 10/3=3.33... truncated to 3.
Example 2
Input: 7, 2
Output: 3
Explanation: 7/2=3.5 truncated to 3.
Example 3
Input: -10, 3
Output: -3
Explanation: -10/3=-3.33... truncated toward zero → -3.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →