Open In App

Number of squares of side length required to cover an N*M rectangle

Improve
Improve
Like Article
Like
Save
Share
Report

Given three numbers N,M ,a . Find Number of squares of dimension a*a required to cover N*M rectangle. 
Note

  • It’s allowed to cover the surface larger than the rectangle, but the rectangle has to be covered.
  • It’s not allowed to break a square.
  • The sides of squares should be parallel to the sides of the rectangle.

Examples

Input: N = 6, M = 6, a = 4
Output: 4
Input: N = 2, M = 3, a = 1
Output: 6


Approach: An efficient approach is to make an observation and find a formula. The constraint that edges of each square must be parallel to the edges of the rectangle allows to analyze X and Y axes separately, that is, how many squares of length ‘a’ are needed to cover squares of length ‘m’ and ‘n’ and take the product of these two quantities. The number of small squares of side length ‘a’ required to cover ‘m’ sized square are ceil(m/a). Similarly, number of ‘a’ sized squares required to cover ‘n’ sized square are ceil(n/a). 
So, the answer will be ceil(m/a)*ceil(n/a).
Below is the implementation of the above approach: 

C++




// CPP program to find number of squares
// of a*a required to cover n*m rectangle
#include <bits/stdc++.h>
using namespace std;
 
// function to find number of squares
// of a*a required to cover n*m rectangle
int Squares(int n, int m, int a)
{
    return ((m + a - 1) / a) * ((n + a - 1) / a);
}
 
// Driver code
int main()
{
    int n = 6, m = 6, a = 4;
 
    // function call
    cout << Squares(n, m, a);
 
    return 0;
}


C




// C program to find number of squares
// of a*a required to cover n*m rectangle
#include <stdio.h>
 
// function to find number of squares
// of a*a required to cover n*m rectangle
int Squares(int n, int m, int a)
{
    return ((m + a - 1) / a) * ((n + a - 1) / a);
}
 
// Driver code
int main()
{
    int n = 6, m = 6, a = 4;
 
    // function call
    printf("%d",Squares(n, m, a));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.


Java




// Java program to find number of squares
// of a*a required to cover n*m rectangle
import java.util.*;
 
class solution
{
 
// function to find a number of squares
// of a*a required to cover n*m rectangle
static int Squares(int n, int m, int a)
{
 
    return ((m + a - 1) / a) * ((n + a - 1) / a);
 
}
 
// Driver code
public static void main(String arr[])
{
    int n = 6, m = 6, a = 4;
 
    // function call
    System.out.println(Squares(n, m, a));
 
}
 
}
//This code is contributed by Surendra_Gangwar


Python 3




# Python 3 program to find number
# of squares of a*a required to
# cover n*m rectangle
 
# function to find number of
# squares of a*a required to
# cover n*m rectangle
def Squares(n, m, a):
    return (((m + a - 1) // a) *
            ((n + a - 1) // a))
 
# Driver code
if __name__ == "__main__":
    n = 6
    m = 6
    a = 4
 
    # function call
    print(Squares(n, m, a))
 
# This code is contributed
# by ChitraNayal


C#




// CSHARP program to find number of squares
// of a*a required to cover n*m rectangle
 
using System;
 
class GFG
{
    // function to find a number of squares
    // of a*a required to cover n*m rectangle
    static int Squares(int n, int m, int a)
    {
     
        return ((m + a - 1) / a) * ((n + a - 1) / a);
     
    }
 
    static void Main()
    {
          int n = 6, m = 6, a = 4;
 
         // function call
         Console.WriteLine(Squares(n, m, a));
    }
    // This code is contributed by ANKITRAI1
}


Javascript




<script>
 
// JavaScript program to find number of squares
// of a*a required to cover n*m rectangle
 
// function to find a number of squares
// of a*a required to cover n*m rectangle
function Squares(n, m, a)
{
    return parseInt(((m + a - 1) / a)) *
           parseInt(((n + a - 1) / a));
}
 
// Driver code
var n = 6, m = 6, a = 4;
 
// Function call
document.write(Squares(n, m, a));
       
// This code is contributed by Ankita saini
 
</script>


PHP




<?php
// PHP program to find number of squares
// of a*a required to cover n*m rectangle
 
// function to find number of squares
// of a*a required to cover n*m rectangle
function Squares($n, $m, $a)
{
    return ((int)(($m + $a - 1) / $a)) *
           ((int)(($n + $a - 1) / $a));
}
 
// Driver code
$n = 6; $m = 6; $a = 4;
 
// function call
echo Squares($n, $m, $a);
 
// This code is contributed
// by Akanksha Rai
?>


Output

4








Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.

Approach#2: Using math

The approach of this algorithm is to calculate the number of squares required to cover the rows and columns of the given N x M rectangle. Then, the product of the number of squares required for rows and columns is returned as the output.

Algorithm

1. Calculate the number of squares required to cover the rows of the rectangle using the ceil function from the math library, i.e., rows = math.ceil(N / a).
2. Calculate the number of squares required to cover the columns of the rectangle using the ceil function from the math library, i.e., cols = math.ceil(M / a).
3. Return the product of the number of squares required for rows and columns, i.e., rows * cols.

C++




#include <iostream>
#include <cmath> // For using ceil function
 
using namespace std;
 
int num_squares2(int N, int M, int a) {
    // Compute the number of squares required to cover the rows
    int rows = ceil(static_cast<double>(N) / a);
 
    // Compute the number of squares required to cover the columns
    int cols = ceil(static_cast<double>(M) / a);
 
    // Return the product of the number of rows and columns
    return rows * cols;
}
 
int main() {
    int N = 2;
    int M = 3;
    int a = 1;
    cout << num_squares2(N, M, a) << endl;
 
    return 0;
}


Java




import java.lang.Math;
 
public class GFG {
    public static int num_squares2(int N, int M, int a) {
        // Compute the number of squares required to cover the rows
        int rows = (int) Math.ceil((double) N / a);
 
        // Compute the number of squares required to cover the columns
        int cols = (int) Math.ceil((double) M / a);
 
        // Return the product of the number of rows and columns
        return rows * cols;
    }
 
    public static void main(String[] args) {
        int N = 2;
        int M = 3;
        int a = 1;
        System.out.println(num_squares2(N, M, a));
    }
}


Python3




import math
 
def num_squares2(N, M, a):
    # Compute the number of squares required to cover the rows
    rows = math.ceil(N / a)
     
    # Compute the number of squares required to cover the columns
    cols = math.ceil(M / a)
     
    # Return the product of the number of rows and columns
    return rows * cols
N=2
M=3
a=1
print(num_squares2(N, M, a))


C#




using System;
 
public class GFG
{
    public static int NumSquares2(int N, int M, int a)
    {
        // Compute the number of squares required to cover the rows
        int rows = (int)Math.Ceiling((double)N / a);
 
        // Compute the number of squares required to cover the columns
        int cols = (int)Math.Ceiling((double)M / a);
 
        // Return the product of the number of rows and columns
        return rows * cols;
    }
 
    public static void Main(string[] args)
    {
        int N = 2;
        int M = 3;
        int a = 1;
        Console.WriteLine(NumSquares2(N, M, a));
    }
}


Javascript




const num_squares2 = (N, M, a) => {
    // Compute the number of squares required to cover the rows
    const rows = Math.ceil(N / a);
     
    // Compute the number of squares required to cover the columns
    const cols = Math.ceil(M / a);
     
    // Return the product of the number of rows and columns
    return rows * cols;
}
 
const N = 2;
const M = 3;
const a = 1;
console.log(num_squares2(N, M, a));


Output

6









Time complexity:  O(1), which means it is constant time complexity. This is because the algorithm performs a fixed number of operations, which are independent of the input size.

Space complexity:  O(1), which means it uses a constant amount of space. This is because it only stores a fixed number of variables, which are independent of the input size.



Last Updated : 16 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads