From 9dfadbb747ec81778a2772b0a8b0fcc3fbed91c3 Mon Sep 17 00:00:00 2001 From: Pranav Shridhar Date: Thu, 31 Oct 2019 19:04:32 +0530 Subject: [PATCH] added electronics shop problem in algorithms implementation section --- .../Implementation/electronics-shop.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Algorithms/Implementation/electronics-shop.cpp diff --git a/Algorithms/Implementation/electronics-shop.cpp b/Algorithms/Implementation/electronics-shop.cpp new file mode 100644 index 0000000..6bfb97c --- /dev/null +++ b/Algorithms/Implementation/electronics-shop.cpp @@ -0,0 +1,56 @@ +// Problem : Algorithms/Implementation - Electronics Shop +// Author - Pranav Shridhar + +#include +#include +#include + +using namespace std; + +int main() { + int b, n, m, i, j; + int price; + int tot; + int maxBuy = INT32_MIN; + + std::vector keyboards; + std::vector usbs; + + cin >> b >> n >> m; + + for (i = 0; i < n; i++) { + cin >> price; + keyboards.push_back(price); + } + for (i = 0; i < m; i++) { + cin >> price; + usbs.push_back(price); + } + + sort(keyboards.begin(), keyboards.end(), greater()); + sort(usbs.begin(), usbs.end()); + + if (*min_element(keyboards.begin(), keyboards.end()) + + *min_element(usbs.begin(), usbs.end()) > b) { + cout << -1; + return 0; + } + + for (i = 0, j = 0; i < n; i++) { + if (keyboards[i] > b) + continue; + for (; j < m; j++) { + tot = keyboards[i] + usbs[j]; + if (tot <= b) { + if (tot > maxBuy) + maxBuy = tot; + } + else + break; + } + } + + cout << maxBuy; + + return 0; +}