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.

Examples:
Input: N = 3
Output: 1
.
. .
. . .
. . . .
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
Output: 11
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:
C++
// C++ implementation of the approach#include <iostream>using namespace std; // Function to return the count of// rectangles when n is oddint countOdd(int n){ int coun = 0, m, j, i; for (i = n - 2; i >= 1; i--) { if (i & 1) { // Calculating number of dots // in vertical level m = (n - i) / 2; // Calculating number of ways // to select two points in the // horizontal level i j = (i * (i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level coun += j * m; } else { // Calculating number of dots // in vertical level m = ((n - 1) - i) / 2; // Calculating number of ways // to select two points in the // horizontal level i j = (i * (i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level coun += j * m; } } return coun;} // Function to return the count of// rectangles when n is evenint countEven(int n){ int coun = 0, m, j, i; for (i = n - 2; i >= 1; i--) { if (i & 1) { m = ((n - 1) - i) / 2; j = (i * (i + 1)) / 2; coun += j * m; } else { m = (n - i) / 2; j = (i * (i + 1)) / 2; coun += j * m; } } return coun;} // Driver codeint main(){ int n = 5; // If n is odd if (n & 1) cout << countOdd(n); else cout << countEven(n); return 0;} |
Java
// Java implementation of the approachimport java.io.*; class GFG { // Function to return the count of // rectangles when n is odd static int countOdd(int n) { int coun = 0, m, j, i; for (i = n - 2; i >= 1; i--) { if (i >= 1) { // Calculating number of dots // in vertical level m = (n - i) / 2; // Calculating number of ways // to select two points in the // horizontal level i j = (i * (i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level coun += j * m; } else { // Calculating number of dots // in vertical level m = ((n - 1) - i) / 2; // Calculating number of ways // to select two points in the // horizontal level i j = (i * (i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level coun += j * m; } } return coun; } // Function to return the count of // rectangles when n is even static int countEven(int n) { int coun = 0, m, j, i; for (i = n - 2; i >= 1; i--) { if (i >= 1) { m = ((n - 1) - i) / 2; j = (i * (i + 1)) / 2; coun += j * m; } else { m = (n - i) / 2; j = (i * (i + 1)) / 2; coun += j * m; } } return coun; } // Driver code public static void main(String[] args) { int n = 5; // If n is odd if (n >= 1) System.out.println(countOdd(n)); else System.out.println(countEven(n)); }} // This code is contributed by Tushil.. |
Python3
# Python 3 implementation of the approach # Function to return the count of# rectangles when n is odddef countOdd(n): coun = 0 i = n - 2 while (i >= 1): if (i & 1): # Calculating number of dots # in vertical level m = int((n - i) / 2) # Calculating number of ways # to select two points in the # horizontal level i j = int((i * (i + 1)) / 2) # Multiply both to obtain the number of # rectangles formed at that level coun += j * m else: # Calculating number of dots # in vertical level m = int(((n - 1) - i) / 2) # Calculating number of ways # to select two points in the # horizontal level i j = int((i * (i + 1)) / 2) # Multiply both to obtain the number of # rectangles formed at that level coun += j * m i -= 1 return coun # Function to return the count of# rectangles when n is evendef countEven(n): coun = 0 i = n - 2 while(i >= 1): if (i & 1): m = int(((n - 1) - i) / 2) j = int((i * (i + 1)) / 2) coun += j * m else: m = int((n - i) / 2) j = (i * (i + 1)) // 2 coun += j * m return coun # Driver codeif __name__ == '__main__': n = 5 # If n is odd if (n & 1): print(countOdd(n)) else: print(countEven(n)) # This code is contributed by# Surendra_Gangwar |
C#
// C# implementation of the approachusing System; class GFG { // Function to return the count of // rectangles when n is odd static int countOdd(int n) { int coun = 0, m, j, i; for (i = n - 2; i >= 1; i--) { if (i >= 1) { // Calculating number of dots // in vertical level m = (n - i) / 2; // Calculating number of ways // to select two points in the // horizontal level i j = (i * (i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level coun += j * m; } else { // Calculating number of dots // in vertical level m = ((n - 1) - i) / 2; // Calculating number of ways // to select two points in the // horizontal level i j = (i * (i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level coun += j * m; } } return coun; } // Function to return the count of // rectangles when n is even static int countEven(int n) { int coun = 0, m, j, i; for (i = n - 2; i >= 1; i--) { if (i >= 1) { m = ((n - 1) - i) / 2; j = (i * (i + 1)) / 2; coun += j * m; } else { m = (n - i) / 2; j = (i * (i + 1)) / 2; coun += j * m; } } return coun; } // Driver code static public void Main() { int n = 5; // If n is odd if (n >= 1) Console.Write(countOdd(n)); else Console.Write(countEven(n)); }} // This code is contributed by Tushil.. |
PHP
<?php// PHP implementation of the approach // Function to return the count of // rectangles when n is odd function countOdd($n) { $coun = 0; for ($i = $n - 2; $i >= 1; $i--) { if ($i & 1) { // Calculating number of dots // in vertical level $m = ($n - $i) / 2; // Calculating number of ways // to select two points in the // horizontal level i $j = ($i * ($i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level $coun += $j * $m; } else { // Calculating number of dots // in vertical level $m = (($n - 1) - $i) / 2; // Calculating number of ways // to select two points in the // horizontal level i $j = ($i * ($i + 1)) / 2; // Multiply both to obtain the number of // rectangles formed at that level $coun += $j * $m; } } return $coun; } // Function to return the count of // rectangles when n is even function countEven($n) { $coun = 0; for ($i = $n - 2; $i >= 1; $i--) { if ($i & 1) { $m = (($n - 1) - i) / 2; $j = ($i * ($i + 1)) / 2; $coun += $j * $m; } else { $m = ($n - $i) / 2; $j = ($i * ($i + 1)) / 2; $coun += $j * $m; } } return $coun; } // Driver code $n = 5; // If n is odd if ($n & 1) echo countOdd($n); else echo countEven($n); // This code is contributed by Arnab Kundu?> |
11
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


