-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDrawingBook.java
39 lines (33 loc) · 921 Bytes
/
DrawingBook.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
public static int pageCount(int n, int p) {
//count how many steps are required to generate 1 to n pages
int step=n/2+1;
// checking from front
int front=Integer.MAX_VALUE;
// checking from last
int last=Integer.MAX_VALUE;
int j=step;
int i=1;
int k=0;
int l=0;
while(i<=step)
{
int firstF=2*(i-1);
int secondF=2*i-1;
if(firstF==p||secondF==p)
{
front=(int)Math.min(front,k);
}
k++;
int firstL=2*(j-1);
int secondL=2*j-1;
if(firstL==p||secondL==p)
{
last=(int)Math.min(last,l);
}
l++;
j--;
i++;
}
int ans=Math.min(front,last);
return ans;
}