

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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++ code to find the area taken by boxes in a container
Suppose, we have n pairs of boxes that need to be shipped in a square-shaped container. The width of the pair of the boxes is given as a pair (a, b) and they are given in an array 'dimensions'. If we keep the width of the boxes parallel to each other, we have to find out how much area the boxes will take inside the container. We cannot stack the boxes on top of each other. We determine the minimum area required by the two boxes in the container for all the n pairs.
So, if the input is like n = 4, dimensions = {{2, 4}, {3, 6}, {2, 5}, {4, 6}}, then the output will be −
64 25 36 16
Steps
To solve this, we will follow these steps −
res := 0 while n is non-zero, do: a := first value of dimensions[n] b := second value of dimensions[n] res := maximum of (2 * minimum of (a, b) and maximum of a and b) print(res * res) n := n - 1
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, vector<pair<int, int>> dimensions) { int res = 0; while(n--){ int a = dimensions[n].first; int b = dimensions[n].second; int res = max(2 * min(a, b), max(a, b)); cout<< res * res << endl; } } int main() { int n = 4; vector<pair<int, int>> dimensions = {{2, 4}, {3, 6}, {2, 5}, {4, 6}}; solve(n, dimensions); return 0; }
Input
4, {{2, 4}, {3, 6}, {2, 5}, {4, 6}}
Output
64 25 36 16
- Related Questions & Answers
- How to compute the area of a set of bounding boxes in PyTorch?
- How to measure the time taken by a function in Java?
- How to measure time taken by a function in C?
- Actions taken by a kernel to context-switch between processes
- Problem: Time taken by tomatoes to rot in JavaScript
- C++ code to find answers by vowel checking
- Find the number of boxes to be removed in C++
- Time taken by savepoint to perform backup in SAP HANA
- C++ code to find total time to pull the box by rabbit
- Program to find total area covered by two rectangles in Python
- Actions taken by a kernel to context-switch between kernel-level threads.
- Program to find maximum number of boxes we can fit inside another boxes in python
- How to remove the boxes around legend of a plot created by ggplot2 in R?
- Java program to find the area of a rectangle
- Java program to find the area of a triangle
Advertisements