Given an equilateral triangle made of dots (.) joined together to form triangle and an integer n which represents the length of the sides of the triangle. The task is to count the number of rectangles that can be inscribed in the given triangle such that:
Horizontal edges must be parallel to the base of the given triangle.
Rectangle must only be formed by joining the dots i.e. all the four edges of the rectangle must touch the dots on the triangle.
Only distinct rectangles should be counted.
Input: N = 3
. . .
. . . .
The only triangle possible has the top edges
at the two points of the second row and bottom edges
at the 2nd and the 3rd points in the last row.
Input: N = 5
Approach: From the above figure, it is clear that n and n – 1 level doesn’t contribute to make any rectangle, so we start to count rectangle from the n – 2 level. When n is odd and the level at which we are calculating say i is also odd then the difference will be even and so we divide it by 2. This will give us the number of vertical levels between the level n and i which can be used for making rectangles and this is same if both are even as difference of even numbers is even. But when one of them is odd then difference will be odd and so n – 1 level will contribute in selecting vertical levels therefore n – 1 level is used in calculation. To calculate the number of ways by which the two dots can be selected in horizontal level we can use formula for sum of n natural numbers because NC2 = 1 + 2 + 3 + … + (N – 1). Now we multiply the number of ways of choosing two dots in one level by the number of dots in vertical level. This will be our result for that particular level and so we will repeat these steps till last and sum up all the values. Below is the implementation of the above approach:
Please Login to comment...