Given two integers L and B denoting the length and breadth of a rectangle respectively. The task is to calculate the sum of the area of all possible squares that comes into the rectangle.
Examples:
Input: L = 4, B = 3
Output: 54
Input: L = 2, B = 5
Output: 26
The idea is to observe the count of number of squares in a rectangle.

Now, the number of squares of side 1 will be 12 as there will be two cases one as squares of 1-unit sides along the horizontal(3) and second case as squares of 1-unit sides along the vertical(4). That gives us 3*4 = 12 squares.
When the side is 2 units, one case will be as squares of side of 2 units along only one place horizontally and second case as two places vertically. So the number of squares = 6
So we can deduce that,
Number of squares of size 1*1 will be L*B
Number of squares of size 2*2 will be (L-1)(B-1)
Therefore, the number of squares with size
will be:
Number of square of size K = (L-K+1)*(B-K+1)
Therefore, area of total number of squares of size K will be:
Area of total number of square of size K = (L-K+1)*(B-K+1)*K*K
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateAreaSum( int l, int b)
{
int size = 1;
int maxSize = min(l,b);
int totalArea = 0;
for ( int i=1; i <= maxSize; i++)
{
int totalSquares = (l-size+1)*(b-size+1);
int area = totalSquares*size*size;
totalArea += area;
size++;
}
return totalArea;
}
int main()
{
int l = 4, b = 3;
cout<<calculateAreaSum(l,b);
return 0;
}
|
Java
class GFG
{
static int calculateAreaSum( int l, int b)
{
int size = 1 ;
int maxSize = Math.min(l, b);
int totalArea = 0 ;
for ( int i = 1 ; i <= maxSize; i++)
{
int totalSquares = (l - size + 1 ) *
(b - size + 1 );
int area = totalSquares *
size * size;
totalArea += area;
size++;
}
return totalArea;
}
public static void main(String[] args)
{
int l = 4 , b = 3 ;
System.out.println(calculateAreaSum(l, b));
}
}
|
Python 3
def calculateAreaSum(l, b):
size = 1
maxSize = min (l, b)
totalArea = 0
for i in range ( 1 , maxSize + 1 ):
totalSquares = ((l - size + 1 ) *
(b - size + 1 ))
area = (totalSquares *
size * size)
totalArea + = area
size + = 1
return totalArea
if __name__ = = "__main__" :
l = 4
b = 3
print (calculateAreaSum(l,b))
|
C#
using System;
class GFG
{
static int calculateAreaSum( int l,
int b)
{
int size = 1;
int maxSize = Math.Min(l, b);
int totalArea = 0;
for ( int i = 1; i <= maxSize; i++)
{
int totalSquares = (l - size + 1) *
(b - size + 1);
int area = totalSquares *
size * size;
totalArea += area;
size++;
}
return totalArea;
}
public static void Main()
{
int l = 4, b = 3;
Console.Write(calculateAreaSum(l,b));
}
}
|
PHP
<?php
function calculateAreaSum( $l , $b )
{
$size = 1;
$maxSize = min( $l , $b );
$totalArea = 0;
for ( $i = 1; $i <= $maxSize ; $i ++)
{
$totalSquares = ( $l - $size + 1) *
( $b - $size + 1);
$area = $totalSquares *
$size * $size ;
$totalArea += $area ;
$size ++;
}
return $totalArea ;
}
$l = 4;
$b = 3;
echo calculateAreaSum( $l , $b );
?>
|
Javascript
<script>
function calculateAreaSum(l, b)
{
var size = 1;
var maxSize = Math.min(l,b);
var totalArea = 0;
for ( var i=1; i <= maxSize; i++)
{
var totalSquares = (l-size+1)*(b-size+1);
var area = totalSquares*size*size;
totalArea += area;
size++;
}
return totalArea;
}
var l = 4, b = 3;
document.write( calculateAreaSum(l,b));
</script>
|
Complexity Analysis:
- Time complexity: O(min(l,b))
- Auxiliary complexity: O(1)