Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
We are given a right isosceles triangle where we need to find the maximum number of 2×2 squares that can fit inside it. An isosceles triangle has two sides of equal length, and in a right isosceles triangle, the base and height are perpendicular and equal to each other.
The key insight is that from the corner triangular areas, we need to subtract 2 units from the base (and height) before calculating the number of squares. The remaining area can be divided into layers of squares.
Syntax
int numofSquares(int base); // Returns maximum number of 2x2 squares that fit in right isosceles triangle
Algorithm
The approach follows these steps −
- Subtract 2 from the base to account for corner triangular areas
- Divide the remaining base by 2 (since each square has side length 2)
- Use the formula n×(n+1)/2 to count total squares in triangular arrangement
Example
#include <stdio.h>
#include <math.h>
int numofSquares(int b) {
// Remove the extra triangular parts from corners
b = b - 2;
// Since each square has side length 2
b = floor(b / 2);
// Use triangular number formula: n*(n+1)/2
return b * (b + 1) / 2;
}
int main() {
int base = 8;
printf("Base of triangle: %d<br>", base);
printf("Maximum squares that can fit: %d<br>", numofSquares(base));
// Test with another example
base = 12;
printf("\nBase of triangle: %d<br>", base);
printf("Maximum squares that can fit: %d<br>", numofSquares(base));
return 0;
}
Base of triangle: 8 Maximum squares that can fit: 6 Base of triangle: 12 Maximum squares that can fit: 15
How It Works
For base = 12 −
- After removing corner areas: 12 - 2 = 10
- Number of square units along base: 10 / 2 = 5
- Using triangular arrangement formula: 5 × 6 / 2 = 15 squares
Key Points
- Always subtract 2 from the base before calculation
- Each 2×2 square requires 2 units of space along the base
- Squares are arranged in triangular layers within the triangle
- Time complexity: O(1), Space complexity: O(1)
Conclusion
The maximum number of 2×2 squares in a right isosceles triangle is calculated using the formula (b-2)/2 × ((b-2)/2 + 1) / 2, where b is the base length. This efficiently counts squares arranged in triangular layers.
