Number of rectangles with given area in an N*M grid
Last Updated :
30 Jun, 2021
Given three positive integers N, M, and A, the task is to count the number of rectangles with area equal to A present in an M * N grid.
Examples:
Input: N = 2, M = 2, A = 2
Output: 4
Explanation:
In the given grid of size 2 × 2, 2 rectangles of dimension 2 × 1 and 2 rectangles of dimension 1 × 2 can be inscribed.
Therefore, the required output is 4.
Input: N = 2, M = 2, A = 3
Output: 0
Explanation:
The possible rectangles with area A (= 3) are of dimensions either 1 × 3 or 3 × 1.
But, the maximum length of a side in the grid can only be 2. Therefore, no rectangles can be inscribed within the grid.
Approach: The problem can be solved based on the following observations:
The total number of ways to select a segment of length X on the segment of length M is equal to (M - X + 1).
Therefore, the total count of rectangles of size X * Y in the rectangle of size M * N is equal to (M - X + 1) * (N - Y + 1).
Follow the steps below to solve the problem:
- Iterate over the range [1, √A]. For every ith iteration, find all possible values of length and breadth of the rectangles, say { i, (A / i)} or { (A / i), i } within the given grid.
- Iterate over all possible values of length say X and breadth say, Y and increment the count of rectangles by (M - X + 1) * (N - Y + 1).
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of rectangles
// in an M * N grid such that the area of
// the rectangles is equal to A
int count_number(int N, int M, int A)
{
// Stores all possible values of length
// and breadth whose area equal to A
vector<pair<int, int> > v;
// Calculate all divisors of A
for (int i = 1; i * i <= A; i++) {
// If N is divisible by i
if (N % i == 0) {
// Stores length of the rectangle
int length = i;
// Stores breadth of the rectangle
int breadth = A / i;
// If length of rectangle is not
// equal to breadth of rectangle
if (length != breadth) {
// Insert { length, breadth }
v.push_back({ length, breadth });
// Insert { breadth, length }
v.push_back({ breadth, length });
}
else {
// Insert { length, breadth}
// because both are equal
v.push_back({ length, breadth });
}
}
}
// Stores the count of rectangles
// in a grid whose area equal to A
long long total = 0;
// Iterate over all possible
// values of { length, breadth }
for (auto it : v) {
// Stores total count of ways to
// select a segment of length it.first
// on the segment of length M
int num1 = (max(0, M - it.first + 1));
// Stores total count of ways to
// select a segment of length it.second
// on the segment of length N
int num2 = (max(0, N - it.second + 1));
// Update total
total += (num1 * num2);
}
return total;
}
// Drivers Code
int main()
{
// Input
int N = 2, M = 2, A = 2;
// Print the result
cout << count_number(N, M, A) << endl;
}
Java
// Java program of the above approach
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find the count of rectangles
// in an M * N grid such that the area of
// the rectangles is equal to A
static int count_number(int N, int M, int A)
{
// Stores all possible values of length
// and breadth whose area equal to A
Vector<pair> v = new Vector<pair>();
// Calculate all divisors of A
for (int i = 1; i * i <= A; i++)
{
// If N is divisible by i
if (N % i == 0)
{
// Stores length of the rectangle
int length = i;
// Stores breadth of the rectangle
int breadth = A / i;
// If length of rectangle is not
// equal to breadth of rectangle
if (length != breadth)
{
// Insert { length, breadth }
v.add(new pair(length, breadth));
// Insert { breadth, length }
v.add(new pair(breadth, length));
}
else
{
// Insert { length, breadth}
// because both are equal
v.add(new pair(length, breadth));
}
}
}
// Stores the count of rectangles
// in a grid whose area equal to A
int total = 0;
// Iterate over all possible
// values of { length, breadth }
for (pair it : v)
{
// Stores total count of ways to
// select a segment of length it.first
// on the segment of length M
int num1 = (Math.max(0, M - it.first + 1));
// Stores total count of ways to
// select a segment of length it.second
// on the segment of length N
int num2 = (Math.max(0, N - it.second + 1));
// Update total
total += (num1 * num2);
}
return total;
}
// Drivers Code
public static void main(String[] args)
{
// Input
int N = 2, M = 2, A = 2;
// Print the result
System.out.print(count_number(N, M, A) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program of the above approach
# Function to find the count of rectangles
# in an M * N grid such that the area of
# the rectangles is equal to A
def count_number(N, M, A):
# Stores all possible values of length
# and breadth whose area equal to A
v = []
# Calculate all divisors of A
for i in range(1, A + 1):
if i * i > A:
break
# If N is divisible by i
if (N % i == 0):
# Stores length of the rectangle
length = i
# Stores breadth of the rectangle
breadth = A // i
# If length of rectangle is not
# equal to breadth of rectangle
if (length != breadth):
# Insert { length, breadth }
v.append([length, breadth ])
# Insert { breadth, length }
v.append([breadth, length ])
else:
# Insert { length, breadth}
# because both are equal
v.append([length, breadth ])
# Stores the count of rectangles
# in a grid whose area equal to A
total = 0
# Iterate over all possible
# values of { length, breadth }
for it in v:
# Stores total count of ways to
# select a segment of length it.first
# on the segment of length M
num1 = (max(0, M - it[0] + 1))
# Stores total count of ways to
# select a segment of length it.second
# on the segment of length N
num2 = (max(0, N - it[1] + 1))
# Update total
total += (num1 * num2)
return total
# Drivers Code
if __name__ == '__main__':
# Input
N, M, A = 2, 2, 2
# Print the result
print(count_number(N, M, A))
# This code is contributed by mohit kumar 29.
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
public class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find the count of rectangles
// in an M * N grid such that the area of
// the rectangles is equal to A
static int count_number(int N, int M, int A)
{
// Stores all possible values of length
// and breadth whose area equal to A
List<pair> v = new List<pair>();
// Calculate all divisors of A
for (int i = 1; i * i <= A; i++)
{
// If N is divisible by i
if (N % i == 0)
{
// Stores length of the rectangle
int length = i;
// Stores breadth of the rectangle
int breadth = A / i;
// If length of rectangle is not
// equal to breadth of rectangle
if (length != breadth)
{
v.Add(new pair(length, breadth));
// Insert { breadth, length }
v.Add(new pair(breadth, length));
}
else
{
// Insert { length, breadth}
// because both are equal
v.Add(new pair(length, breadth));
}
}
}
// Stores the count of rectangles
// in a grid whose area equal to A
int total = 0;
// Iterate over all possible
// values of { length, breadth }
foreach (pair it in v)
{
// Stores total count of ways to
// select a segment of length it.first
// on the segment of length M
int num1 = (Math.Max(0, M - it.first + 1));
// Stores total count of ways to
// select a segment of length it.second
// on the segment of length N
int num2 = (Math.Max(0, N - it.second + 1));
// Update total
total += (num1 * num2);
}
return total;
}
// Driver code
public static void Main(String[] args)
{
// Input
int N = 2, M = 2, A = 2;
// Print the result
Console.Write(count_number(N, M, A) +"\n");
}
}
// This code is contributed by susmitakundugoaldang
JavaScript
<script>
// Javascript program of the above approach
class pair
{
constructor(first,second)
{
this.first = first;
this.second = second;
}
}
function count_number(N,M,A)
{
// Stores all possible values of length
// and breadth whose area equal to A
let v = [];
// Calculate all divisors of A
for (let i = 1; i * i <= A; i++)
{
// If N is divisible by i
if (N % i == 0)
{
// Stores length of the rectangle
let length = i;
// Stores breadth of the rectangle
let breadth = A / i;
// If length of rectangle is not
// equal to breadth of rectangle
if (length != breadth)
{
// Insert { length, breadth }
v.push(new pair(length, breadth));
// Insert { breadth, length }
v.push(new pair(breadth, length));
}
else
{
// Insert { length, breadth}
// because both are equal
v.push(new pair(length, breadth));
}
}
}
// Stores the count of rectangles
// in a grid whose area equal to A
let total = 0;
// Iterate over all possible
// values of { length, breadth }
for (let it=0;it< v.length;it++)
{
// Stores total count of ways to
// select a segment of length it.first
// on the segment of length M
let num1 = (Math.max(0, M - v[it].first + 1));
// Stores total count of ways to
// select a segment of length it.second
// on the segment of length N
let num2 = (Math.max(0, N - v[it].second + 1));
// Update total
total += (num1 * num2);
}
return total;
}
// Drivers Code
// Input
let N = 2, M = 2, A = 2;
// Print the result
document.write(count_number(N, M, A) +"<br>");
// This code is contributed by unknown2108
</script>
Time Complexity: O(sqrt(N))
Auxiliary Space: O(sqrt(N))
Similar Reads
Number of rectangles in N*M grid We are given a N*M grid, print the number of rectangles in it.Examples: Input : N = 2, M = 2Output : 9There are 4 rectangles of size 1 x 1.There are 2 rectangles of size 1 x 2There are 2 rectangles of size 2 x 1There is one rectangle of size 2 x 2.Input : N = 5, M = 4Output : 150Input : N = 4, M = 3
7 min read
Number of squares of maximum area in a rectangle Given a rectangle of sides m and n. Cut the rectangle into smaller identical pieces such that each piece is a square having maximum possible side length with no leftover part of the rectangle. Print number of such squares formed.Examples: Input: 9 6 Output: 6 Rectangle can be cut into squares of siz
6 min read
Find minimum area of rectangle with given set of coordinates Given an array arr of set of points in the X-Y plane. The task is to find the minimum area of a rectangle that can be formed from these points. The sides of the rectangle should be parallel to the X and Y axes. If a rectangle cannot be formed with the given points then print 0 .Examples: Input: arr[
5 min read
Find the number of corner rectangles that can be formed from given Matrix Given a binary matrix mat[][] of dimensions N*M, the task is to find the number of corner rectangles that can be formed. A corner rectangle is defined as the submatrix having 1s on the corners of it and each 1s must belong to a unique cell in that submatrix. Examples: Input: mat[][] = {{1, 0, 1}, {0
7 min read
Create a matrix with alternating rectangles of O and X Write a code which inputs two numbers m and n and creates a matrix of size m x n (m rows and n columns) in which every elements is either X or 0. The Xs and 0s must be filled alternatively, the matrix should have outermost rectangle of Xs, then a rectangle of 0s, then a rectangle of Xs, and so on. E
15 min read
Find the minimum number of rectangles left after inserting one into another Given width and height of N rectangles. The task is to find the minimum number of rectangles left after inserting one into another. Note : If W1 < W2 and H1 < H2 then rectangle 1 fits inside rectangle 2.The smallest rectangle can insert in the second smallest, and this rectangle can insert in
9 min read
Coordinates of rectangle with given points lie inside Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of an obtained recta
6 min read
Find number of rectangles that can be formed from a given set of coordinates Given an array arr[][] consisting of pair of integers denoting coordinates. The task is to count the total number of rectangles that can be formed using given coordinates. Examples:Input: arr[][] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}, {11, 11}}Output: 3Explanation: Following are the rect
6 min read
Sum of the count of number of adjacent squares in an M X N grid Given an M Ã N matrix. The task is to count the number of adjacent cells and calculate their sum. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally.Examples : Input : m = 2, n = 2 Output : 12Input : m = 3, n = 2 Output: 22See the below diag
3 min read
Number of squares in a rectangle Given a m x n matrix, count the number of squares in the matrix.Examples : Input: m = 2, n = 2Output: 5Explanation: There are 4 squares of size 1x1 + 1 square of size 2x2.Input: m = 4, n = 3Output: 20Explanation: There are 12 squares of size 1x1 + 6 squares of size 2x2 + 2 squares of size 3x3.[Naive
7 min read