forked from somya1212/dynamicProgramming-Important-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimumOperations.java
58 lines (50 loc) · 1.02 KB
/
minimumOperations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
You are given a number N. You have to find the number of operations
required to reach N from 0. You have 2 operations available:
Double the number
Add one to the number
Example:
Input:
2
8
7
Input:
4
5
Explanation:
Testcase1:
Input : N = 8
Output : 4
0 + 1 = 1, 1 + 1 = 2, 2 * 2 = 4, 4 * 2 = 8
Testcase2:
Input : N = 7
Output : 5
0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, 3 * 2 = 6, 6 + 1 = 7
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class main {
public static void array(int a[],int n)
{
a[0]=0;a[1]=1;a[2]=2;
for(int j=3;j<n;j++)
{
if(j%2==1)
a[j]=a[j-1]+1;
else
a[j]=a[j/2]+1;
}
}
public static void main (String[] args) throws IOException{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[10001];
array(a,10001);
int t=Integer.parseInt(bf.readLine());
for(int i=0;i<t;i++)
{
int n=Integer.parseInt(bf.readLine());
System.out.println(a[n]);
}
}
}