Codingame Solution: Divisibility windows


Given two numbers d and x, the aim is to find a and b the two numbers divisible by d around and closest to x, i.e.:

a is the largest multiple of d such that a < x,
b is the smallest multiple of d such that b > x.
Line 1 : d
Line 2 : x
Line 1 : a and b space-separated
x is never divisible by d
d < x
2 <= d <= 65536
3 <= x <= 2147418111


The problem statement asks to find \(a, b\) such that \(a<x<b\) and \(d | a,b\). Since \(d<x\) and \(d\not | x\), it's enough to scale \(x\) down by \(d\) and the integer part of that result \(r=\left\lfloor\frac{x}{d}\right\rfloor \) must be the maximum, such that \(a=r \cdot d\).

For the same argument, we can say that \(s=\left\lceil\frac{x}{d}\right\rceil \), such that \(b=s \cdot d\) or even simpler \(b= a+ d\)

const d = +readline();
const x = +readline();

print([Math.floor(x / d) * d, Math.ceil(x / d) * d].join(" "))

« Back to problem overview