# Maximum number of squares that can fit in a right angle isosceles triangle

You are given an isosceles (a triangle with at-least two equal sides) right angle triangle with base b, we need to find the maximum number of squares of side m, which can be fitted into given triangle.

Examples:

Input : b = 6, m = 2 Output : 3 Input : b = 4, m = 1 Output : 6

Let’s consider a right angle triangle XYZ, where YZ is the base of triangle. Suppose length of the base is b. If we consider the position of first square with the vertex Y, we will have (b / m-1) squares in the base, and we will be left with another isosceles right angle triangle having base length (b – m).

** Illustration : **

Let f(b, m) = Number of squares which can be fitted in triangle having base length b.

then **f(b, m) = (b / m – 1) + f(b – m, m)**

We can calculate f(b) using above recursion, and with use of memoization. Later we can answer each query in O(1) time. We can do it for even and odd numbers separately with the base case if (b < 2 * m) f(b, m) = 0.

The given recursion can be solved as :

f(b, m) = b / m – 1 + f(b – m, m) = b / m – 1 + (b – m) / m – 1 + f(b – 2m, m)

f(b, m) = b / m – 1 + b / m – 2 + f(b – 3m, m) +…+ f(b – (b / m)m, m)

f(b) = b / m – 1 + b / m – 2 + b / m – 3 +…..+ 1 + 0

With conditions, if (b < 2 * m) f(b, m) = 0

f(b) = sum of first (b / m – 1) natural numbers

= (b / m – 1) * (b / m) / 2

This formula can be used to reduce the time complexity upto O(1).

## C++

`// CPP program for finding maximum squares ` `// that can fit in right angle isosceles ` `// triangle ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function for finding max squares ` `int` `maxSquare(` `int` `b, ` `int` `m) ` `{ ` ` ` `// return in O(1) with derived ` ` ` `// formula ` ` ` `return` `(b / m - 1) * (b / m) / 2; ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `b = 10, m = 2; ` ` ` `cout << maxSquare (b,m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for finding maximum squares ` `// that can fit in right angle isosceles ` `// triangle ` `public` `class` `GFG ` `{ ` ` ` `// function for finding max squares ` ` ` `static` `int` `maxSquare(` `int` `b, ` `int` `m) ` ` ` `{ ` ` ` `// return in O(1) with derived ` ` ` `// formula ` ` ` `return` `(b / m - ` `1` `) * (b / m) / ` `2` `; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `b = ` `10` `, m = ` `2` `; ` ` ` `System.out.println(maxSquare (b,m)); ` ` ` `} ` `} ` ` ` `// This code is contribute by Sumit Ghosh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for ` `# finding maximum squares ` `# that can fit in ` `# right angle isosceles ` `# triangle ` ` ` `# function for finding max squares ` `def` `maxSquare(b, m): ` ` ` ` ` `# return in O(1) with derived ` ` ` `# formula ` ` ` `return` `(b ` `/` `m ` `-` `1` `) ` `*` `(b ` `/` `m) ` `/` `2` ` ` ` ` `# driver program ` `b ` `=` `10` `m ` `=` `2` `print` `(` `int` `(maxSquare (b,m))) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program for finding maximum squares ` `// that can fit in right angle isosceles ` `// triangle ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// function for finding max squares ` ` ` `static` `int` `maxSquare(` `int` `b, ` `int` `m) ` ` ` `{ ` ` ` `// return in O(1) with derived ` ` ` `// formula ` ` ` `return` `(b / m - 1) * (b / m) / 2; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `b = 10, m = 2; ` ` ` `Console.WriteLine(maxSquare (b, m)); ` ` ` `} ` `} ` ` ` `// This code is contribute by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for finding ` `// maximum squares that can ` `// fit in right angle isosceles ` `// triangle ` ` ` `// function for finding ` `// max squares ` `function` `maxSquare(` `$b` `, ` `$m` `) ` `{ ` ` ` ` ` `// return in O(1) with ` ` ` `// derived formula ` ` ` `return` `(` `$b` `/ ` `$m` `- 1) * ` ` ` `(` `$b` `/ ` `$m` `) / 2; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$b` `= 10; ` `$m` `= 2; ` ` ` `echo` `maxSquare(` `$b` `,` `$m` `); ` `// This code is contribute by vt_m ` `?> ` |

*chevron_right*

*filter_none*

Output:

10

This article is contributed by **Shivam Pradhan (anuj_charm)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Find the altitude and area of an isosceles triangle
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Find two vertices of an isosceles triangle in which there is rectangle with opposite corners (0, 0) and (X, Y)
- Find other two sides of a right angle triangle
- Area of a largest square fit in a right angle triangle
- Find other two sides and angles of a right angle triangle
- Number of squares of maximum area in a rectangle
- Maximum sum of a path in a Right Number Triangle
- Number of Isosceles triangles in a binary tree
- Exterior angle of a cyclic quadrilateral when the opposite interior angle is given
- Angle between a chord and a tangent when angle in the alternate segment is given
- Maximum path sum in a triangle.
- Maximum path sum in an Inverted triangle | SET 2