- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum number of pieces in N cuts in C++
Given the task is to calculate the maximum number of square or rectangle pieces of equal size that can be obtained by cutting a given square piece in total N number of cuts horizontally or vertically.
Let’s now understand what we have to do using an example −
Input − N=8
Output − 25
Explanation − When N=8, the number of vertical cuts = 4 and the number of horizontal cuts = 4.
Total pieces = 25
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 |
Input − 7
Output − 20
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 |
Approach used in the below program as follows
If N is the number of cuts and we have to maximize the resultant pieces then equal number of horizontal and vertical cuts have to be made.
If N is even then there will be equal horizontal and vertical cuts else if N is odd, then the horizontal cuts will be 1 more than the vertical cuts or vice versa.
Therefore, the number of horizontal = N/2 and vertical cuts = N-H.
In function MaxPieces() initialize a variable H =N/2 of type int to store the number of Horizontal cuts.
Initialize another variable V=N-H of type int to store the number of vertical cuts.
Final number of pieces = (Horizonal Rows)*(Vertical rows) = (H+1)*(V+1)
Example
#include <bits/stdc++.h> using namespace std; int MaxPieces(int N){ //H is the number of horizontal cuts int H = N / 2; //V is the number of vertical cuts int V = N-H; // maximum number of pieces = (H+1)*(V+1) return ((H + 1) * (V + 1)); } //Main function int main(){ //Number of cuts int N = 7; cout << "Max pieces = "<<MaxPieces(N); return 0; }
Output
If we run the above code we will get the following output −
20
- Related Articles
- Count pieces of the circle after N cuts in C++
- Possible cuts of a number such that maximum parts are divisible by 3 in C++
- Maximum number of ones in a N*N matrix with given constraints in C++
- Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts in C++
- Maximum number of dots after throwing a dice N times in C++
- Minimum and Maximum number of pairs in m teams of n people in C++
- Maximum points of intersection n circles in C++
- Maximum points of intersection n lines in C++
- Maximum Number of Ones in C++
- Probability that the pieces of a broken stick form a n sided polygon in C++
- Maximum GCD of N integers with given product in C++
- Create Maximum Number in C++
- Third Maximum Number in C++
- Maximum occurred integer in n ranges using C++
- Maximum number that can be display on Seven Segment Display using N segments in C++
