33. Closest Multiple
Difficulty: easy · Math
Problem
Given two integers `x` and `y`, find the multiple of `y` that is closest to `x`. If two multiples are equally close (one below `x` and one above), print the one with the larger absolute value.
Input
- Line 1: the integer `x`. - Line 2: the integer `y`.
Output
Print the multiple of `y` closest to `x`.
Example 1
Input: 15 4 Output: 16
Explanation: Multiples of 4 near 15 are 12 (distance 3) and 16 (distance 1), so the answer is 16.
Example 2
Input: 14 4 Output: 16
Explanation: 12 and 16 are both at distance 2; 16 has the larger absolute value.
Example 3
Input: -14 4 Output: -16
Explanation: -12 and -16 are both at distance 2; -16 has the larger absolute value.
Constraints
- -10^5 < x < 10^5 - 1 <= y <= 10^5
Solutions are judged against 3 sample and 12 hidden tests. Sign in to solve it · All problems