You are viewing a single comment's thread. Return to all comments →
My solution in Java 7. Hope it helps . :)
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int p = in.nextInt(); int first = fromFirst(n,p); int last = fromLast(n,p); System.out.println(first > last ? last : first); } public static int fromFirst(int n, int p){ if(p == 1){ return 0; }else{ return p % 2 == 0 ? p/2 : (p-1)/2; } } public static int fromLast(int n, int p){ if(n % 2 == 0){ if(p % 2 == 0){ return (n-p)/2; }else{ return (n-(p-1))/2; } }else if(n-1 == p){ return 0; }else{ return p % 2 == 0 ? ((n-1)-p)/2 : ((n-1)-(p-1))/2; } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Drawing Book
You are viewing a single comment's thread. Return to all comments →
My solution in Java 7. Hope it helps . :)