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

29 Nov, 2021
What is the maximum number of squares of size 2×2 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
Please refer below diagram for explanation.
Input : 7
Output : 3
Since the triangle is isosceles, the given base would also be equal to the height. Now in the diagonal part, we would always need an extra length of 2 units in both height and base of the triangle to accommodate a triangle. (The CF and AM segment of the triangle in the image. The part that does not contribute to any square). In the remaining length of base, we can construct length / 2 squares. Since each square is of 2 units, same would be the case of height, there is no need to calculate that again. 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++
Java
Python3
C#
PHP
Javascript
// C++ program to count number of 2 x 2// squares in a right isosceles triangle#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 codeint main(){   int base = 8;   cout << numberOfSquares(base);   return 0;}// This code is improved by heroichitesh.
// Java program to count number of 2 x 2// squares in a right isosceles triangle  class Squares{  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));   }}  // This code is contributed by Anshika Goyal and improved by heroichitesh.
# Python3 program to count number# of 2 x 2 squares in a right # isosceles triangledef 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      # Driver codebase = 8  print(numberOfSquares(base))  # This code is contributed by Anant Agarwal and improved by heroichitesh. 
// C# program to count number of 2 x 2// squares in a right isosceles triangleusing 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;    }          // Driver code    public static void Main()    {                  int _base = 8;        Console.WriteLine(numberOfSquares(_base));    }}  // This code is contributed by anuj_67.
<?php// PHP program to count number of 2 x 2// squares in a right isosceles triangle      function numberOfSquares( $base)    {                  // removing the extra         // part we would        // always need        $base = ($base - 2);                  // Since each square        // has base of        // length of 2        $base = intdiv($base, 2);              return $base * ($base + 1)/2;    }  // Driver code$base = 8;echo numberOfSquares($base);  // This code is contributed by anuj_67 and improved by heroichitesh.?>
<script>      // Program to count number of 2 x 2     // squares in a right isosceles triangle           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;     }           // Driver code     let base = 8;     document.write(numberOfSquares(base));           // This code is contributed by Mayank Tyagi  </script>
Output: 
6
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
vt_m
HeroicHitesh
mayanktyagi1709
triangle
Geometric
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Closest Pair of Points | O(nlogn) Implementation
Convex Hull | Set 2 (Graham Scan)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Given n line segments, find if any two segments intersect
Sum of Manhattan distances between all pairs of points
Convex Hull using Divide and Conquer Algorithm
Program for Point of Intersection of Two Lines
Equation of circle when three points on the circle are given