Given a square piece and a total number of cuts available n, Find out the maximum number of rectangular or square pieces of equal size that can be obtained with n cuts. The allowed cuts are horizontal and vertical cut.
Note: Stacking and folding is not allowed.
Examples:
Input : n = 1
Output : 2
Explanation :

Input : n = 2
Output : 4
Explanation :

Input : n = 3
Output : 6
Explanation :

Given is n which is the number of allowed cuts. As it is required to maximize number of pieces after n cuts, So number of horizontal cuts will be equal to number of vertical cuts. This can be prove using differentiation. So number of horizontal cut will be n/2. and vertical cuts will be n-n/2.
So number of pieces = (horizontal cut + 1) * (vertical cut + 1).
Program:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaximumPieces( int n)
{
int x = n / 2;
return ((x + 1) * (n - x + 1));
}
int main()
{
int n = 3;
cout << "Max number of pieces for n = " << n
<< " is " << findMaximumPieces(3);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int findMaximumPieces( int n)
{
int x = n / 2 ;
return ((x + 1 ) * (n - x + 1 ));
}
public static void main (String[] args)
{
int n = 3 ;
System.out.print( "Max number of pieces for n = " +
n + " is " + findMaximumPieces( 3 ));
}
}
|
Python 3
def findMaximumPieces(n):
x = n / / 2
return ((x + 1 ) * (n - x + 1 ))
if __name__ = = "__main__" :
n = 3
print ( "Max number of pieces for n = " + str ( n)
+ " is " + str (findMaximumPieces( 3 )))
|
C#
using System;
class GFG
{
public static int findMaximumPieces( int n)
{
int x = n / 2;
return ((x + 1) * (n - x + 1));
}
static public void Main ()
{
int n = 3;
Console.Write( "Max number of pieces for n = " +
n + " is " + findMaximumPieces(3));
}
}
|
PHP
<?php
function findMaximumPieces( $n )
{
$x = (int)( $n / 2);
return (( $x + 1) * ( $n - $x + 1));
}
$n = 3;
echo "Max number of pieces for n = " .
$n . " is " . findMaximumPieces(3);
?>
|
Javascript
<script>
function findMaximumPieces(n)
{
var x = parseInt(n / 2);
return ((x + 1) * (n - x + 1));
}
var n = 3;
document.write( "Max number of pieces for n = " + n
+ " is " + findMaximumPieces(3));
</script>
|
Output:
Max number of pieces for n = 3 is 6
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!