Added 10 solutions

This commit is contained in:
Rodney Shaghoulian
2017-09-11 09:32:17 -07:00
parent 1b2f98dd95
commit ba2c70755b
81 changed files with 517 additions and 74 deletions
@@ -0,0 +1,31 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
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);
}
}