Thursday, October 10, 2024
Google search engine
HomeData Modelling & AIFind the side of the squares which are lined in a row,...

Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given

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.
Examples: 
 

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

 

Approach: 
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) 
side of the square = distance between the first and last squares/(no. of squares given - 1)
 

C++




// C++ program to find side of the squares
// which 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(int n, int d)
{
    cout << "The side of each square is "
         << d / (n - 1) << endl;
}
 
// Driver code
int main()
{
    int d = 42, n = 4;
    radius(n, d);
    return 0;
}


Java




// Java program to find side of the squares
// which 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(int n, int d)
{
    System.out.print( "The side of each square is "
        + d / (n - 1));
}
 
// Driver code
public static void main (String[] args)
{
    int d = 42, n = 4;
    radius(n, d);
}
}
 
// This code is contributed by vt_m.


Python3




     
# Python program to find side of the squares
# which 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));
 
 
d = 42; n = 4;
radius(n, d);
 
 
 
# This code contributed by PrinciRaj1992


C#




// C# program to find side of the squares
// which are lined in a row and distance between the
// centers of first and last squares is given
using System;
 
class GFG
{
 
static void radius(int n, int d)
{
    Console.Write( "The side of each square is "
        + d / (n - 1));
}
 
// Driver code
public static void Main ()
{
    int d = 42, n = 4;
    radius(n, d);
}
}
 
// This code is contributed by anuj_67..


Javascript




<script>
// javascript program to find side of the squares
// which are lined in a row and distance between the
// centers of first and last squares is given
function radius(n , d)
{
    document.write( "The side of each square is "
        + d / (n - 1));
}
 
// Driver code
var d = 42, n = 4;
radius(n, d);
 
// This code is contributed by 29AjayKumar
</script>


Output: 

The side of each square is 14

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
31 May, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments