Count the number of rhombi possible inside a rectangle of given size

Given a rectangle of height H and width W which has the bottom left corner at (0, 0). The task is to count the number of distinct Rhombi that have all points inside or on the border of the rectangle satisfying the following conditions exists:

Approach: Since the diagonals are parallel to the axis, let’s try fixing the diagonals and creating rhombi on them. For the rhombus to have integer coordinates, the length of the diagonals must be even. Let’s fix the length of the diagonals to i and j, the number of rhombi we can form with these diagonal lengths inside the rectangle would be (H – i + 1) * (W – j + 1). Thus, we iterate over all possible values of i and j and update the count.

Below is the implementation of the above approach:

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ implementation of the approach

#include <bits/stdc++.h>

usingnamespacestd;

// Function to return the count of rhombi possible

longlongcountRhombi(inth, intw)

{

longlongct = 0;

// All possible diagonal lengths

for(inti = 2; i <= h; i += 2)

for(intj = 2; j <= w; j += 2)

// Update rhombi possible with

// the current diagonal lengths

ct += (h - i + 1) * (w - j + 1);

// Return the total count

// of rhombi possible

returnct;

}

// Driver code

intmain()

{

inth = 2, w = 2;

cout << countRhombi(h, w);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java implementation of the approach

importjava.io.*;

classGFG

{

// Function to return the count of rhombi possible

staticintcountRhombi(inth, intw)

{

intct = 0;

// All possible diagonal lengths

for(inti = 2; i <= h; i += 2)

for(intj = 2; j <= w; j += 2)

// Update rhombi possible with

// the current diagonal lengths

ct += (h - i + 1) * (w - j + 1);

// Return the total count

// of rhombi possible

returnct;

}

// Driver code

publicstaticvoidmain (String[] args)

{

inth = 2, w = 2;

System.out.println (countRhombi(h, w));

}

}

// This code is contributed by jit_t

chevron_right

filter_none

Python 3

filter_none

editclose

play_arrow

linkbrightness_4code

# Python 3 implementation of the approach

# Function to return the count of

# rhombi possible

defcountRhombi(h, w):

ct =0;

# All possible diagonal lengths

fori inrange(2, h +1, 2):

forj inrange(2, w +1, 2):

# Update rhombi possible with

# the current diagonal lengths

ct +=(h -i +1) *(w -j +1)

# Return the total count

# of rhombi possible

returnct

# Driver code

if__name__ =="__main__":

h =2

w =2

print(countRhombi(h, w))

# This code is contributed by ita_c

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find the frequency of

// minimum element in the array

usingSystem;

classGFG

{

// Function to return the count

// of rhombi possible

staticintcountRhombi(inth, intw)

{

intct = 0;

// All possible diagonal lengths

for(inti = 2; i <= h; i += 2)

for(intj = 2; j <= w; j += 2)

// Update rhombi possible with

// the current diagonal lengths

ct += (h - i + 1) * (w - j + 1);

// Return the total count

// of rhombi possible

returnct;

}

// Driver code

publicstaticvoidMain()

{

inth = 2, w = 2;

Console.WriteLine(countRhombi(h, w));

}

}

// This code is contributed by Ryuga

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP implementation of the approach

// Function to return the count of

// rhombi possible

functioncountRhombi($h, $w)

{

$ct= 0;

// All possible diagonal lengths

for($i= 2; $i<= $h; $i+= 2)

for($j= 2; $j<= $w; $j+= 2)

// Update rhombi possible with

// the current diagonal lengths

$ct+= ($h- $i+ 1) * ($w- $j+ 1);

// Return the total count

// of rhombi possible

return$ct;

}

// Driver code

$h= 2; $w= 2;

echo(countRhombi($h, $w));

// This code is contributed by Code_Mech

?>

chevron_right

filter_none

Output:

1

Time Complexity: O(H * W)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.