Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
The equation to calculate no. of draws, when I started from the front of the book:
no. of draws, when I started from the back of the book:
if
is even
else
I created solution in:
All solutions are also available on my GitHub profile.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import scala.io.Source object Solution extends App { val lines = Source.stdin.getLines().toList val n = lines.head.toInt val p = lines.tail.head.toInt val fromFront = p val fromBack = if(n % 2 == 0) { n + 1 } else { n } val result = List(fromFront / 2, (fromBack - p) / 2).min println(result) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.*; public class Solution { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); int p = stdin.nextInt(); int fromFront = p; int fromBack = n; if(n % 2 == 0) { fromBack = n + 1; } int result = Math.min(fromFront / 2, (fromBack - p) / 2); System.out.println(result); stdin.close(); } } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
'use strict'; const processData = input => { const [n, p] = input.split('\n').map(i => parseInt(i)); const fromFront = p; let fromBack = n; if(n % 2 == 0) { fromBack = n + 1; } const result = parseInt(Math.min(fromFront / 2, (fromBack - p) / 2)); console.log(result); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |