-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
29 lines (24 loc) · 858 Bytes
/
Solution.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
// github.com/RodneyShag
import java.util.Scanner;
// Explanation:
// - The last page is n/2 page turns away.
// - It takes p/2 pages to get to the p-th page (from the front)
// - It takes n/2 - p/2 pages to get to the p-th page (from the back)
// - The minimum of p/2 and (n/2 - p/2) is the minimum number of pages Brie must turn
// Time complexity: O(1)
// Space complexity: O(1)
public class Solution {
static int solve (int n, int p) {
int pagesFromFront = p/2;
int pagesFromBack = n/2 - p/2;
return Math.min(pagesFromFront, pagesFromBack);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int p = scan.nextInt();
scan.close();
int result = solve(n, p);
System.out.println(result);
}
}