forked from RyanFehr/HackerRank
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
Latest commit
56 lines (43 loc) · 965 Bytes
/
Copy pathSolution.java
File metadata and controls
56 lines (43 loc) · 965 Bytes
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
//Problem: https://www.hackerrank.com/challenges/drawing-book
//Java 8
/*
Initial Thoughts:
Choose the minimum turns it takes
to get to page p from the
beginning and the end of the book
page/2 gives us the turns
Example:
6
_1 23 45 6_
0 1 1 0
7
_1 23 45 67
0 1 1 0
8
_1 23 45 67 8_
0 1 2 1 0
We can see from examples that
for odd pages we will need to use
ceil to get the proper # of turns
Time complexity: O(1)
Space complexity: O(1)
*/
importjava.io.*;
importjava.util.*;
importjava.text.*;
importjava.math.*;
importjava.util.regex.*;
publicclassSolution {
publicstaticvoidmain(String[] args) {
Scannerin = newScanner(System.in);
intn = in.nextInt();
intp = in.nextInt();
intbeg = (p/2);
intend = 0;
if(n%2==1)
end = (n-p)/2;
else
end = (int) Math.ceil((n-p)/2.0);
System.out.println(Math.min(beg,end));
}
}