- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Program to find number of given operations required to reach Target in Python

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 −

## Example

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))

## Input

2, 7

## Output

3

- 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++
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to count number of 5-star reviews required to reach threshold percentage in Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to find number of ways to form a target string given a dictionary in Python
- C++ Program to find out the minimum number of operations required to defeat an enemy

Advertisements