Given here are **n** squares which are inclined and touch each other externally at vertices, 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.

**Examples:**

Input :d = 42, n = 4Output :The side of each square is 9.899

Input :d = 54, n = 7Output :The side of each square is 6.364

**Approach**:

There are n squares each having side of length a and the distance between the first and last squares is equal to d. From the figure, it is clear that they are connected by diagonals. Length of each diagonal is equal to **a√2**.

For the first and last square, only half of the diagonal is covered under the length d.For rest of the (n-2) squares, the complete diagonal is covered in d. Hence the relation between a and d is as follows:

a/√2 + a/√2 + (n-2)*a√2 = d

=> a√2 + √2na – 2a√2 = d

=> n√2a – a√2 = d

=> a = d/((n-1)*(√2))

Side of the square = distance between centers/((no. of squares-1) * sqrt(2)).

Below is the implementation of the above approach:

`// C++ program to find side of the squares ` `// inclined and touch each other externally ` `// at vertices and are lined in a row ` `// and distance between the ` `// centers of first and last squares is given ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `void` `radius(` `double` `n, ` `double` `d) `
`{ ` ` ` `cout << ` `"The side of each square is "`
` ` `<< d / ((n - 1) * ` `sqrt` `(2)) << endl; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `double` `d = 42, n = 4; `
` ` `radius(n, d); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java program to find side of the squares ` `// inclined and touch each other externally ` `// at vertices and are lined in a row ` `// and distance between the ` `// centers of first and last squares is given ` `import` `java.io.*; `
` ` `class` `GFG `
`{ ` ` ` ` ` `static` `void` `radius(` `double` `n, ` `double` `d) `
`{ ` ` ` `System.out.print( ` `"The side of each square is "` `+ `
` ` `d / ((n - ` `1` `) * Math.sqrt(` `2` `))); `
`} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) `
`{ ` ` ` `double` `d = ` `42` `, n = ` `4` `; `
` ` `radius(n, d); `
`} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

`# Python program to find side of the squares ` `# inclined and touch each other externally ` `# at vertices and are lined in a row ` `# and distance between the ` `# centers of first and last squares is given ` ` ` `def` `radius(n, d): `
` ` ` ` `print` `(` `"The side of each square is "` `, `
` ` `d ` `/` `((n ` `-` `1` `) ` `*` `(` `2` `*` `*` `(` `1` `/` `2` `)))); `
` ` `# Driver code ` `d ` `=` `42` `; n ` `=` `4` `; `
`radius(n, d); ` ` ` `# This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

`// C# program to find side of the squares ` `// inclined and touch each other externally ` `// at vertices and are lined in a row ` `// and distance between the ` `// centers of first and last squares is given ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` ` ` `static` `void` `radius(` `double` `n, ` `double` `d) `
`{ ` ` ` `Console.WriteLine( ` `"The side of each square is "` `+ `
` ` `d / ((n - 1) * Math.Sqrt(2))); `
`} ` ` ` `// Driver code ` `public` `static` `void` `Main () `
`{ ` ` ` `double` `d = 42, n = 4; `
` ` `radius(n, d); `
`} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

The side of each square is 9.89949

** Time Complexity : ** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Find the Side of the smallest Square that can contain given 4 Big Squares
- Find the vertex diagonally opposite to the vertex M from an N-sided polygon
- Number of squares of side length required to cover an N*M rectangle
- Count squares with odd side length in Chessboard
- Diagonally Dominant Matrix
- Print matrix elements diagonally in spiral form
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Count number less than N which are product of perfect squares
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Find the area of rhombus from given Angle and Side length
- Find coordinates of the triangle given midpoint of each side
- Program to find third side of triangle using law of cosines
- Program to find the side of the Octagon inscribed within the square
- Find K numbers with sum equal to N and sum of their squares maximized
- Minimum and maximum possible length of the third side of a triangle
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.