मान लीजिए, हमारे पास n जोड़े बॉक्स हैं जिन्हें वर्गाकार कंटेनर में भेजना है। बॉक्स के जोड़े की चौड़ाई एक जोड़ी (a, b) के रूप में दी गई है और वे सरणी 'dimensions' में दिए गए हैं। यदि हम बॉक्स की चौड़ाई को एक दूसरे के समानांतर रखते हैं, तो हमें यह पता लगाना है कि बॉक्स कंटेनर के अंदर कितना क्षेत्रफल लेंगे। हम बॉक्स को एक दूसरे के ऊपर नहीं रख सकते। हम कंटेनर में दो बॉक्स के लिए आवश्यक न्यूनतम क्षेत्रफल निर्धारित करते हैं, सभी n जोड़े के लिए।
उदाहरण के लिए, यदि इनपुट n = 4, dimensions = {{2, 4}, {3, 6}, {2, 5}, {4, 6}} है, तो आउटपुट होगा:
64 25 36 16
चरण
इसे हल करने के लिए, हम निम्नलिखित चरणों का पालन करेंगे:
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
उदाहरण
बेहतर समझ के लिए निम्नलिखित कार्यान्वयन देखें:
#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; }
इनपुट
4, {{2, 4}, {3, 6}, {2, 5}, {4, 6}}
आउटपुट
64 25 36 16