Maximum of smallest possible area that can get with exactly k cut of given rectangular
Last Updated :
29 Dec, 2022
Given n×m big rectangular area with unit squares and k times cut is allowed, the cut should be straight (horizontal or vertical) and should go along the edges of the unit square. What is the maximum possible area of the smallest piece he can get with exactly k cuts.
Examples :
Input : 3 4 1
Output : 6
Input : 6 4 2
Output : 8
Image for 2nd input
Image for 1st input
As this is n×m rectangular area so there are (n-1) rows and (m-1) columns . So if k > (n + m – 2) , then cut are not possible . Then, if k is less than that . There will be two cases
- When k is less than max( n, m ) – 1 : In the 1st case, if k is less than max( n, m ) – 1, then either m * ( n / ( k+1 ) ) or n * ( m / ( k+1 ) ) is maximum, here we have divided by ( k + 1) because horizontally or vertically i.e ( m * n = total blocks ) is divided into ( k + 1 ) parts.
- When k is greater than or equal to max( n, m) – 1 : In the 2nd case, if k >= max( n, m ) – 1, then there will be cut on both rows as well as columns so, the maximum possible smallest area will be either m / ( k – n + 2) or n / ( k – m + 2 ) . In this case suppose if n > m then, firstly n-1 (row or column) cut is possible . After that (k – n) cut will be done on m – 1 . So, here we have adjusted this ( k – n ) cut such that smallest possible division should be maximum.
Code – Below is the implementation of the following approach
C++
#include <bits/stdc++.h>
using namespace std;
void max_area( int n, int m, int k)
{
if (k > (n + m - 2))
cout << "Not possible" << endl;
else {
int result;
if (k < max(m, n) - 1) {
result = max(m * (n / (k + 1)), n * (m / (k + 1)));
}
else {
result = max(m / (k - n + 2), n / (k - m + 2));
}
cout << result << endl;
}
}
int main()
{
int n = 3, m = 4, k = 1;
max_area(n, m, k);
}
|
Java
class GFG {
static void max_area( int n, int m, int k)
{
if (k > (n + m - 2 ))
System.out.println( "Not possible" );
else {
int result;
if (k < Math.max(m, n) - 1 )
{
result = Math.max(m * (n / (k + 1 )),
n * (m / (k + 1 )));
}
else {
result = Math.max(m / (k - n + 2 ),
n / (k - m + 2 ));
}
System.out.println(result);
}
}
public static void main (String[] args)
{
int n = 3 , m = 4 , k = 1 ;
max_area(n, m, k);
}
}
|
Python3
def max_area(n,m,k):
if (k > (n + m - 2 )):
print ( "Not possible" )
else :
if (k < max (m,n) - 1 ):
result = max (m * (n / (k + 1 )), n * (m / (k + 1 )));
else :
result = max (m / (k - n + 2 ), n / (k - m + 2 ));
print (result)
n = 3
m = 4
k = 1
max_area(n, m, k)
|
C#
using System;
class GFG {
static void max_area( int n, int m, int k)
{
if (k > (n + m - 2))
Console.WriteLine( "Not possible" );
else {
int result;
if (k < Math.Max(m, n) - 1)
{
result = Math.Max(m * (n / (k + 1)),
n * (m / (k + 1)));
}
else {
result = Math.Max(m / (k - n + 2),
n / (k - m + 2));
}
Console.WriteLine(result);
}
}
public static void Main ()
{
int n = 3, m = 4, k = 1;
max_area(n, m, k);
}
}
|
PHP
<?php
function max_area( $n , $m , $k )
{
if ( $k > ( $n + $m - 2))
echo "Not possible" , "\n" ;
else
{
$result ;
if ( $k < max( $m , $n ) - 1)
{
$result = max( $m * ( $n / ( $k + 1)),
$n * ( $m / ( $k + 1)));
}
else
{
$result = max( $m / ( $k - $n + 2),
$n / ( $k - $m + 2));
}
echo $result , "\n" ;
}
}
$n = 3; $m = 4; $k = 1;
max_area( $n , $m , $k );
?>
|
Javascript
<script>
function max_area(n, m, k)
{
if (k > (n + m - 2))
document.write( "Not possible" );
else
{
let result;
if (k < Math.max(m, n) - 1)
{
result = Math.max(m * (n / (k + 1)),
n * (m / (k + 1)));
}
else
{
result = Math.max(m / (k - n + 2),
n / (k - m + 2));
}
document.write(result);
}
}
let n = 3, m = 4, k = 1;
max_area(n, m, k);
</script>
|
Output :
6
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...