Check if an area P can be obtained from an area of N * M
Last Updated :
05 May, 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++
#include <bits/stdc++.h>
using namespace std;
void splitArea( int n, int m, int p)
{
if (n > m)
swap(n, m);
vector< int > factors;
for ( int i = 1; i * i <= p; i++) {
if (p % i == 0) {
factors.push_back(i);
}
}
for ( int i = 0; i < ( int )factors.size();
i++) {
if (factors[i] <= n &&
p / factors[i] <= m) {
cout << "YES" ;
return ;
}
}
cout << "NO" ;
}
int main()
{
int n = 3, m = 3, p = 4;
splitArea(n, m, p);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void swap( int m, int n)
{
int temp = m;
m = n;
n = temp;
}
static void splitArea( int n, int m, int p)
{
if (n > m)
swap(n, m);
ArrayList<Integer> factors = new ArrayList<Integer>();
for ( int i = 1 ; i * i <= p; i++)
{
if (p % i == 0 )
{
factors.add(i);
}
}
for ( int i = 0 ; i < ( int )factors.size();
i++)
{
if (factors.get(i) <= n &&
p / factors.get(i) <= m)
{
System.out.print( "YES" );
return ;
}
}
System.out.print( "NO" );
}
public static void main(String[] args)
{
int n = 3 , m = 3 , p = 4 ;
splitArea(n, m, p);
}
}
|
Python3
def splitArea(n, m, p):
if (n > m):
n, m = m, n
factors = []
for i in range ( 1 , p + 1 ):
if (p % i = = 0 ):
factors.append(i)
for i in range ( len (factors)):
if (factors[i] < = n and p / / factors[i] < = m):
print ( "YES" )
return
print ( "NO" )
if __name__ = = '__main__' :
n, m, p = 3 , 3 , 4
splitArea(n, m, p)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void swap( int m, int n)
{
int temp = m;
m = n;
n = temp;
}
static void splitArea( int n, int m, int p)
{
if (n > m)
swap(n, m);
List< int > factors = new List< int >();
for ( int i = 1; i * i <= p; i++)
{
if (p % i == 0)
{
factors.Add(i);
}
}
for ( int i = 0; i < ( int )factors.Count;
i++)
{
if (factors[i] <= n &&
p / factors[i] <= m)
{
Console.Write( "YES" );
return ;
}
}
Console.Write( "NO" );
}
public static void Main(String[] args)
{
int n = 3, m = 3, p = 4;
splitArea(n, m, p);
}
}
|
Javascript
<script>
function swap(m, n) {
var temp = m;
m = n;
n = temp;
}
function splitArea(n, m, p) {
if (n > m) swap(n, m);
var factors = new Array();
for ( var i = 1; i * i <= p; i++) {
if (p % i == 0) {
factors.push(i);
}
}
for ( var i = 0; i < factors.length; i++) {
if (factors[i] <= n && p / factors[i] <= m) {
document.write( "YES" );
return ;
}
}
document.write( "NO" );
}
var n = 3,
m = 3,
p = 4;
splitArea(n, m, p);
</script>
|
Time Complexity: O(√P)
Auxiliary Space: O(log(P))
Share your thoughts in the comments
Please Login to comment...