Count of distinct rectangles inscribed in an equilateral triangle
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++
#include <iostream>
using namespace std;
int countOdd( int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i & 1) {
m = (n - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
else {
m = ((n - 1) - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
}
return coun;
}
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;
}
int main()
{
int n = 5;
if (n & 1)
cout << countOdd(n);
else
cout << countEven(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countOdd( int n)
{
int coun = 0 , m, j, i;
for (i = n - 2 ; i >= 1 ; i--) {
if (i >= 1 ) {
m = (n - i) / 2 ;
j = (i * (i + 1 )) / 2 ;
coun += j * m;
}
else {
m = ((n - 1 ) - i) / 2 ;
j = (i * (i + 1 )) / 2 ;
coun += j * m;
}
}
return coun;
}
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;
}
public static void main(String[] args)
{
int n = 5 ;
if (n >= 1 )
System.out.println(countOdd(n));
else
System.out.println(countEven(n));
}
}
|
Python3
def countOdd(n):
coun = 0
i = n - 2
while (i > = 1 ):
if (i & 1 ):
m = int ((n - i) / 2 )
j = int ((i * (i + 1 )) / 2 )
coun + = j * m
else :
m = int (((n - 1 ) - i) / 2 )
j = int ((i * (i + 1 )) / 2 )
coun + = j * m
i - = 1
return coun
def 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
if __name__ = = '__main__' :
n = 5
if (n & 1 ):
print (countOdd(n))
else :
print (countEven(n))
|
C#
using System;
class GFG {
static int countOdd( int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
m = (n - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
else {
m = ((n - 1) - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
}
return coun;
}
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;
}
static public void Main()
{
int n = 5;
if (n >= 1)
Console.Write(countOdd(n));
else
Console.Write(countEven(n));
}
}
|
PHP
<?php
function countOdd( $n )
{
$coun = 0;
for ( $i = $n - 2; $i >= 1; $i --)
{
if ( $i & 1)
{
$m = ( $n - $i ) / 2;
$j = ( $i * ( $i + 1)) / 2;
$coun += $j * $m ;
}
else
{
$m = (( $n - 1) - $i ) / 2;
$j = ( $i * ( $i + 1)) / 2;
$coun += $j * $m ;
}
}
return $coun ;
}
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 ;
}
$n = 5;
if ( $n & 1)
echo countOdd( $n );
else
echo countEven( $n );
?>
|
Javascript
<script>
function countOdd(n)
{
let coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
m = parseInt((n - i) / 2, 10);
j = parseInt((i * (i + 1)) / 2, 10);
coun += j * m;
}
else {
m = parseInt(((n - 1) - i) / 2, 10);
j = parseInt((i * (i + 1)) / 2, 10);
coun += j * m;
}
}
return coun;
}
function countEven(n)
{
let coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
m = parseInt(((n - 1) - i) / 2, 10);
j = parseInt((i * (i + 1)) / 2, 10);
coun += j * m;
}
else {
m = parseInt((n - i) / 2, 10);
j = parseInt((i * (i + 1)) / 2, 10);
coun += j * m;
}
}
return coun;
}
let n = 5;
if (n >= 1)
document.write(countOdd(n));
else
document.write(countEven(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...