Robert Eisele
Engineer, Systems Architect and DBA

Codingame: Simultaneous Ages

Goal

Y years ago, Person A was M times older than Person B. Currently, Person A is older than Person B by N years. What are their ages now?
Input
Line 1: 3 integers: N M Y
Output
Line 1: A B, Person A and B's age respectively.
Constraints
0 < B < A < 100,000
0 < NMY < 100,000

Solution

The current situation is that $$A = B + N$$. $$Y$$ years ago, the situation was $$A = B \cdot M$$, which means that:

$\begin{array}{rrl}& A-Y =& (B - Y) M\\\Leftrightarrow & B + N - Y =& B M - Y M\\\Leftrightarrow & B =& \frac{Y - Y M - N}{1 - M}\\\Leftrightarrow & B =& Y+\frac{ N}{M - 1}\end{array}$

Finally, $$B$$ can be used for the first formula. Implementing the routine in JavaScript then looks as follows:

var inputs = readline().split(' ');
var N = +inputs[0];
var M = +inputs[1];
var Y = +inputs[2];

var B = Y + N / (M - 1);
var A = B + N;
print(A + " " + B);

« Back to problem overview