- Related Questions & Answers
- Program to find minimum number of buses required to reach final target in python
- Program to find number of combinations of coins to reach target in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of operations required to make one number to another in Python
- Finding minimum number of required operations to reach n from m in JavaScript
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find number of operations required to remove palindromic sublists in C++
- Program to find number of sublists whose sum is given target in python
- Program to find minimum number of operations required to make one string substring of other in Python
- Count the number of operations required to reduce the given number in C++
- Program to count minimum number of operations to flip columns to make target in Python
- Program to count number of operations required to all cells into same color in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to count number of operations required to convert all values into same in Python?
- Program to find minimum steps to reach target position by a chess knight in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two values start and end, we have to find the minimum number of operations needed to convert start to end by using these operations −

Decrement by 1

Multiply by 2

So, if the input is like start = 2, end = 7, then the output will be 3, as we can multiply 2 to get 4, then multiply 2 to get 8 and then subtract 1 to get 7.

To solve this, we will follow these steps −

ans := 0

Do the following infinitely, do

if end <= start, then

return ans + start − end

otherwise when end is odd, then

end := end + 1, ans := ans + 1

otherwise,

end := quotient of end / 2

ans := ans + 1

Let us see the following implementation to get better understanding −

class Solution: def solve(self, start, end): ans = 0 while True: if end <= start: return ans + start - end elif end % 2: end += 1 ans += 1 else: end //= 2 ans += 1 ob1 = Solution() start = 2 end = 7 print(ob1.solve(start, end))

2, 7

3

Advertisements