Open In App

Maximum number of 2x2 squares that can be fit inside a right isosceles triangle

Last Updated : 25 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

What is the maximum number of squares of size 2x2 units that can be fit in a right-angled isosceles triangle of a given base (in units). 
A side of the square must be parallel to the base of the triangle. 

Examples: 

Input : 8
Output : 6
Explanation: We get total 6 squares ( 1 + 2 + 3). Please refer the below diagram.

Input : 7
Output : 3

For an isosceles triangle, the base is equal to the height. To accommodate a triangle in the diagonal, we need an extra 2 units for both the height and base (for example, the CF and AM segments in the above diagram do not contribute to any square).
In the remaining length of the base, we can fit (length - 2)/2 squares since each square has a side of 2 units. This same logic applies to the height, so no further calculation is needed there.
So, for each level of given length we can construct "(length-2)/2" squares. This gives us a base of "(length-2)" above it. Continuing this process to get the no of squares for all available "length-2" height, we can calculate the squares. 

while length > 2
answer += (length - 2 )/2
length = length - 2

For more effective way, we can use the formula of sum of AP n * ( n + 1 ) / 2, where n = length - 2 

C++
#include<bits/stdc++.h>
using namespace std;

int numberOfSquares(int base)
{
   // removing the extra part we would
   // always need
   base = (base - 2);

   // Since each square has base of
   // length of 2
   base = floor(base / 2);

   return base * (base + 1)/2;
}

// Driver code
int main()
{
   int base = 8;
   cout << numberOfSquares(base);
   return 0;
}
Java
class GfG
{
  public static  int numberOfSquares(int base)
   {
      // removing the extra part 
      // we would always need
      base = (base - 2);
 
      // Since each square has 
      // base of length of 2
      base = Math.floorDiv(base, 2);
 
      return base * (base + 1)/2;
   }
 
   // Driver code
   public static void main(String args[])
   {
      
      int base = 8;
      System.out.println(numberOfSquares(base));
   }
}
Python
def numberOfSquares(base):
    
    # removing the extra part we would
    # always need
    base = (base - 2)
    
    # Since each square has base of
    # length of 2
    base = base // 2
    
    return base * (base + 1) / 2
    
base = 8
print(numberOfSquares(base))
C#
using System;

class GfG {
    
    public static int numberOfSquares(int _base)
    {
        
        // removing the extra part we would always need
        _base = (_base - 2);
    
        // Since each square has 
        // base of length of 2
        _base = _base / 2;
        
    
        return _base * (_base + 1)/2;
    }

    public static void Main()
    {
        
        int _base = 8;
        Console.WriteLine(numberOfSquares(_base));
    }
}
JavaScript
function numberOfSquares(base) 
    { 
        
        // Removing the extra part we would always need 
        base = (base - 2); 
        
        // Since each square has base of 
        // length of 2 
        base = Math.floor(base / 2); 
        
        return base * (base + 1) / 2; 
    } 

    let base = 8; 
    console.log(numberOfSquares(base)); 

Output: 

6

Time complexity : O(1)
Auxiliary Space : O(1)


Next Article

Similar Reads