Minimum Operations

Given a number N the task is to find an optimal solution to reach N from 0 using 2 operations ie
1. Double the number
2. Add one to the number

Example

Input  : N = 8
Output : 4
0 + 1 = 1, 1 + 1 = 2, 2 * 2 = 4, 4 * 2 = 8

Input  : N = 7
Output : 5
0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, 3 * 2 = 6, 6 + 1 = 7

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N.

Output:
For each test case in a new line print an integer denoting the min no of operation to be performed to reach N from 0.

Constraints:
1<=T<=100
1<=N<=10000

Example:
Input:

2
8
7
Input:
4
5

**For More Examples Use Expected Output**

Disclaimer: This content belongs to geeksforgeeks, source: http://geeksforgeeks.org

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar