- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Given the task is to find the maximum number of squares having side ‘a’ that can fit inside a give right angle isosceles triangle with base of ‘s’(An isosceles triangle has at least 2 equal sides).

Let’s now understand what we have to do using an example:

## Input

s=5, a=1

## Output

10

**Explanation** − The number of squares in the base can be calculated by dividing s by a and subtracting 1. So the number of squares in base = 5/1 – 1 = 4.

Similarly when the bottom 4 squares are placed then we get a new isosceles triangle with base (s-a) and then we repeat the same procedure and get 3 squares and so on until a single square is placed at the top.

## Input

s=7, a=2

## Output

3

## Approach used in the below program as follows

To find the maximum number of squares we will have to start from the base of the triangle and find out the number of squares.

To find the number of squares we will divide the base s by the side of the square a and subtract 1 from it = s/a – 1.

Then it will leave us another isosceles triangle with base (s - a) which will accommodate one less square than the previous row below it which we can calculate in the following way −

Squares in next row = (s - a)/a – 1 = (s/a – a/a) – 1= s/a - 1 - 1 = s/a – 2 = one square less than the previous row.

The number of squares keep decreasing until it reaches 1, therefore we have to only find the number of squares in the base row and use the formula of adding natural numbers to find the final sum which is −

(n) * (n + 1) / 2

In this case the formula becomes − ((s / a) – 1) * (s / a) / 2

## Example

#include<bits/stdc++.h> using namespace std; int Max(int s, int a){ //formula for calculating maximum squares return ((s / a) - 1) * (s / a) / 2; } //Main function int main(){ int s = 5, a = 1; cout <<"Maximum squares possible are: "<<Max(s,a); return 0; }

## Output

10

- Related Questions & Answers
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- Maximum sum of a path in a Right Number Triangle in C++
- Find other two sides of a right angle triangle in C++
- Find other two sides and angles of a right angle triangle in C++
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle?
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle in C?
- Program to count number of isosceles triangle from colored vertex regular polygon in Python
- Python program to find angle between mid-point and base of a right angled triangle
- Find the altitude and area of an isosceles triangle in C++
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Get the Least-squares fit of a polynomial to data in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Find maximum number that can be formed using digits of a given number in C++
- Maximum number of threads that can be created within a process in C