Divide Real

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Question- Divide 2 Numbers (Binary

Search)

Problem Description
You are given two number. A dividend and one divisor. We
have two divide them by using Binary Search.
Example: -
INPUT – DIVIDEND = 18
DIVISOR = 6
OUTPUT – 3

Our memory allocation.

What are we going to do??


We are going to put binary search on the elements before the
dividend. So, elements before dividend are our search space.
We use a small mathematical logic. And we will be done with
this problem.
What will be our solution approach??
1. Take input of dividend and divisor.

2. Make function and call it.


3. Inside the function.
4. We will now initialise all the variable needed for binary
search.
The initial position of the pointers.

5. Here We will be using dividend and divisor as absolute. At


the last we will see the signs.
6. Now first condition. Which say product of mid and divisor
is equal to dividend.
If true then answer = mid. And the break.

If false then move to another condition.

7. Now second condition. Which says product of mid and


divisor is bigger than dividend.
If true than calculate the end and mid again. And update
the answer.
8. Now else condition. Which says answer will be updated
and start will also be updated and mid also.
9. And this works vice -versa till it gets an appropriate
answer.
10. Now we will see the sign of the dividend and divisor.
It is simple math condition so you can understand it.
11. And answer is printed.
I hope you will like the explanation of my code. I have not
made it lengthy. So, I have not written the basic instructions.

So, Till then bye………

You might also like