Skip to content
Related Articles

Related Articles

Check if an area P can be obtained from an area of N * M
  • Last Updated : 02 Mar, 2021

Given integers N, M and P, the task is to check if is possible to find a rectangular area of P sq. units inside a rectangular area of N × M sq units.

Examples:

Input: N = 3, M = 3, P = 4
Output: YES
Explanation: Rectangle of 2 x 2 sq. unit area

Input: N = 4, M = 4, P = 7
Output: NO

Approach: Follow the steps below to solve the problem



  • Find all the factors of p and store them in the vector, say factors.
  • Maintain order N ≤ M.
  • Traverse the vector factors.
    • For each array element factors[i], check if factors[i] ≤ N and p / factors[i] ≤ M, where factors[i] and p / factors[i] represent dimensions of rectangular area.
    • If found to be true, print YES and return.
  • Otherwise, print NO

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a rectangle
// of p sq units can be formed from
//  an area of n * m sq units
void splitArea(int n, int m, int p)
{
    // Maintain order n <= m
    if (n > m)
        swap(n, m);
 
    vector<int> factors;
 
    // Iterate to find factors of p
    for (int i = 1; i * i <= p; i++) {
 
        // p is divisible by i
        if (p % i == 0) {
            factors.push_back(i);
        }
    }
 
    for (int i = 0; i < (int)factors.size();
         i++) {
 
        // Check if dimensions
        // lie in given area
        if (factors[i] <= n &&
            p / factors[i] <= m) {
 
            cout << "YES";
            return;
        }
    }
 
    cout << "NO";
}
 
// Driver Code
int main()
{
    int n = 3, m = 3, p = 4;
    splitArea(n, m, p);
}

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to swap two numbers
  // Using temporary variable
  static void swap(int m, int n)
  {
     
    // Swapping the values
    int temp = m;
    m = n;
    n = temp;
  }
 
  // Function to check if a rectangle
  // of p sq units can be formed from
  //  an area of n * m sq units
  static void splitArea(int n, int m, int p)
  {
 
    // Maintain order n <= m
    if (n > m)
      swap(n, m);
    ArrayList<Integer> factors = new ArrayList<Integer>();
 
    // Iterate to find factors of p
    for (int i = 1; i * i <= p; i++)
    {
 
      // p is divisible by i
      if (p % i == 0)
      {
        factors.add(i);
      }
    }
 
    for (int i = 0; i < (int)factors.size();
         i++)
    {
 
      // Check if dimensions
      // lie in given area
      if (factors.get(i) <= n &&
          p / factors.get(i) <= m)
      {
        System.out.print("YES");
        return;
      }
    }
 
    System.out.print("NO");
  }
 
 
// Driver code
public static void main(String[] args)
{
    int n = 3, m = 3, p = 4;
    splitArea(n, m, p);
}
}
 
// This code is contributed by code_hunt.

Python3




# Python3 program for the above approach
 
# Function to check if a rectangle
# of p sq units can be formed from
# an area of n * m sq units
def splitArea(n, m, p):
   
    # Maintain order n <= m
    if (n > m):
        n, m = m, n
    factors = []
 
    # Iterate to find factors of p
    for i in range(1, p + 1):
 
        # p is divisible by i
        if (p % i == 0):
            factors.append(i)
 
    for i in range(len(factors)):
       
        # Check if dimensions
        # lie in given area
        if (factors[i] <= n and p // factors[i] <= m):
            print("YES")
            return
 
    print("NO")
 
# Driver Code
if __name__ == '__main__':
    n, m, p = 3, 3, 4
    splitArea(n, m, p)
 
    # This code is contributed by mohit kumar 29.

C#




// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
   
  // Function to swap two numbers
  // Using temporary variable
  static void swap(int m, int n)
  {
     
    // Swapping the values
    int temp = m;
    m = n;
    n = temp;
  }
 
  // Function to check if a rectangle
  // of p sq units can be formed from
  //  an area of n * m sq units
  static void splitArea(int n, int m, int p)
  {
 
    // Maintain order n <= m
    if (n > m)
      swap(n, m);
    List<int> factors = new List<int>();
 
    // Iterate to find factors of p
    for (int i = 1; i * i <= p; i++)
    {
 
      // p is divisible by i
      if (p % i == 0)
      {
        factors.Add(i);
      }
    }
 
    for (int i = 0; i < (int)factors.Count;
         i++)
    {
 
      // Check if dimensions
      // lie in given area
      if (factors[i] <= n &&
          p / factors[i] <= m)
      {
        Console.Write("YES");
        return;
      }
    }
 
    Console.Write("NO");
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int n = 3, m = 3, p = 4;
    splitArea(n, m, p);
  }
}
 
// This code is contributed by splevel62.

 
 

Output: 
YES

 

 

Time Complexity: O(√P)
Auxiliary Space: O(log(P))

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :