- 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

Suppose we have a number start and another number end (start < end), we have to find the minimum number of operations required to convert start to end using these operations −

- Increment by 1
- Multiply by 2

So, if the input is like start = 5, end = 11, then the output will be 2, as we can multiply 2 to get 10, then add 1 to get 11.

To solve this, we will follow these steps −

- ct:= 0
- while end/2 >= start, do
- if end mod 2 is same as 1, then
- end := end - 1
- end := end/2
- ct := ct + 2

- otherwise,
- end:= end/2
- ct := ct + 1

- if end mod 2 is same as 1, then
- ct := ct +(end-start)
- return ct

Let us see the following implementation to get better understanding −

class Solution: def solve(self, start, end): ct=0 while(end/2>=start): if end%2==1: end-=1 end=end/2 ct+=2 else: end=end/2 ct+=1 ct+=(end-start) return ct ob = Solution() print(ob.solve(5,11))

5,11

2

- Related Questions & Answers
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of operations to make string sorted in Python
- Program to find number of steps required to change one word to another in Python
- Minimum number of given operations required to make two strings equal using C++.
- Program to find number of given operations required to reach Target in Python
- Program to find minimum one bit operations to make integers zero in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Number of operations required to make all array elements Equal in Python
- Program to count number of swaps required to change one list to another in Python?
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to find number of operations required to remove palindromic sublists in C++

Advertisements