Given here are n squares which touch each other externally, and are lined up in a row. The distance between the centers of the first and last square is given. The squares have equal side length. The task is to find the side of each square.
Input: d = 42, n = 4 Output: The side of each square is 14 Input: d = 36, n = 5 Output: The side of each square is 9
Suppose there are n squares each having side of length a.
Let, the distance between the first and last squares = d
From the figure, it is clear,
a/2 + a/2 + (n-2)*a = d
a + na – 2a = d
na – a = d
so, a = d/(n-1)
The side of each square is 14
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.