- 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
C++ Program to get minimum perimeter of rectangle whose area is n
Suppose we have a number n. We are developing a project to build a new data center. The plot of this data center will be a rectangle with an area of exactly n square meters. Each side of the data center must be an integer. We want to minimize the impact of the external environment on the data center. For this reason, we want to minimize the length of the perimeter of the data center (that is, the sum of the lengths of its four sides). We have to find the minimum perimeter of a rectangular data center with an area of exactly n square meters.
Problem Category
Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.
So, if the input of our problem is like n = 36, then the output will be 24, because the required shape of the data center will be 6×6 square. And the perimeter is 6 + 6 + 6 + 6 = 24.
Steps
To solve this, we will follow these steps −
ans := inf for initialize i := 1, when i * i <= n, update (increase i by 1), do: if n mod i is same as 0, then: tmp := n / i ans := minimum of ans and 2 * (tmp + i) return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n){ int ans = 9999; for (int i = 1; i * i <= n; i++){ if (n % i == 0){ int tmp = n / i; ans = min(ans, 2 * (tmp + i)); } } return ans; } int main(){ int n = 36; cout << solve(n) << endl; }
Input
36
Output
24
- Related Articles
- C Program for Area And Perimeter Of Rectangle
- C++ Program to get maximum area of rectangle made from line segments
- Maximum area of rectangle possible with given perimeter in C++
- Program to calculate area and perimeter of a rhombus whose diagonals are given\nWhat is rhombus in C++?
- Program to find Perimeter / Circumference of Square and Rectangle in C++
- Calculate the area and perimeter of the following shapes:Q.1 Calculate the area of Square and rectangleQ.2 Calculate perimeter of Square and rectangle."\n
- Find minimum area of rectangle with given set of coordinates in C++
- Find the area of a square park whose perimeter is 320m.
- What is perimeter of rectangle?
- Java Program to Find the Perimeter of a Rectangle
- Swift Program to Find the Perimeter of a Rectangle
- Kotlin Program to Find the Perimeter of a Rectangle
- The length of a rectangle is 50 cm if the perimeter of the rectangle is 150 cm. Find the area of the rectangle.
- Find the perimeter of the square whose area is $2500 m^{2}$.
- Find the area of a square park whose perimeter is $320 m$.
