Maximum number of pieces in N cuts
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)
Last Updated :
08 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...