Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given two integers one is a dividend and the opposite is the divisor, we have to discover the quotient when the dividend is split by the divisor with out using any ” / “ and ” % “ operators.
Examples:
Enter: dividend = 10, divisor = 2
Output: 5
Clarification: 10/2 = 5.Enter: dividend = 10, divisor = 3
Output: 3
Clarification: 10/3 = 3.33333… which is truncated to three.Enter: dividend = 10, divisor = -2
Output: -5
Clarification: 10/-2 = -5.
Method: To resolve the issue utilizing Binary Search observe the beneath thought:
We already know that Quotient * Divisor ≤ Dividend and the Quotient lie between 0 and Dividend. Due to this fact, we are able to assume the Quotient as mid, the Dividend as larger certain and 0 because the decrease certain and might simply use binary search to fulfill the phrases of division which is Quotient * Divisor ≤ Dividend.
Comply with the steps to resolve the issue:
Under is the implementation of the above method:
|
The Quotient is : 5
Time Complexity: O(log N), as Binary Search algorithm is used.
Auxiliary House: O(1), since no further house has been used.
[ad_2]