Problem of the day
You are given two integers, ‘dividend’ and ‘divisor’.
You are required to divide the integers without using multiplication, division, and modular operators.
Your task is to return the quotient after dividing ‘dividend’ by ‘divisor’.
Note :
In cases where the dividend is not perfectly divisible by the divisor, you are required to return the integer value of the quotient which is closer to zero.
For example - If the answer is '8.34', we return the integer part, i.e., '8'. Also, If the answer is '-2.67', we return the integer part, i.e., '-2'.
Assume we're dealing with a system that can only store integers in the 32-bit signed integer range: [2^31, 2^31-1]. If the quotient is higher than 2^31 - 1, return 2^31 - 1; if it is less than -2^31, return -2^31.
For example :
If the answer is ‘9.333’, then return ‘9’, or if the answer is ‘-8.123’, then return ‘-8’.
The first input line contains two space-separated integers, ‘dividend’ and ‘divisor’.
Output Format
The only line contains the result after division, following the above rules.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
10 3
3
Given ‘dividend = 10’ and ‘divisor = 3’
So the division is ‘10/3 = 3.3333……’.
Return only integer part ‘3’
7 -3
-2
-10^9 <= dividend, divisor <= 10^9
divisor != 0
Time limit: 1 second
Change multiplicative property as a subtraction.
We can subtract the divisor from the dividend until the dividend is greater than the divisor. The quotient will be equal to the number of total subtractions performed.
Below is the detailed algorithm:
0(N / M), where ‘N’ is ‘dividend’ and ‘M’ is ‘divisor’.
We are using a while loop, at max, it can iterate upto ‘dividend/divisor’ times.
O(1).
Constant space is used.