You are viewing a single comment's thread. Return to all comments →
Here is my solution: (Just need to analize quadratic inequality, and analize discriminant, there is the trick)
static int solve(int n, int k) { double disc = 1d*n * n - 4d * n * k; if (disc < 0) { return n - 1; } double x1 = (n + Math.sqrt(disc)) / 2; double x2 = (n - Math.sqrt(disc)) / 2;
if (x1 < 1 && x2 < 1) { return 0; } if (x1 > x2) { if (x1 >= n) { if (x2 < 1) { return 0; } else { return (int)x2; } } else { if (x2 < 1) { return (int)(n - x1); } else { return (int)x2 + (int)(n - x1); } } } else if (x1 == x2) { return n - 1; } else { if (x2 >= n) { if (x1 < 1) { return 0; } else { return (int)x1; } } else { if (x1 < 1) { return (int)(n - x2); } else { return (int)x1 + (int)(n - x2); } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Counting
You are viewing a single comment's thread. Return to all comments →
Here is my solution: (Just need to analize quadratic inequality, and analize discriminant, there is the trick)
static int solve(int n, int k) { double disc = 1d*n * n - 4d * n * k; if (disc < 0) { return n - 1; } double x1 = (n + Math.sqrt(disc)) / 2; double x2 = (n - Math.sqrt(disc)) / 2;