Electronics Shop

Solutions A simple solution consists in running two nested for loops to calculate the possible costs. Although this solution is \(O(n \cdot m)\), it’s acceptable since the input is small: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int s, n, m; cin >> s >> n >> m; vector<int> k(n); copy_n(istream_iterator<int>(cin), n, begin(k)); vector<int> u(m); copy_n(istream_iterator<int>(cin), m, begin(u)); auto cost = -1; for(auto i : k) { for (auto j : u) { if (i+j<=s) cost = max(cost, i+j); } } cout << cost; Linq-based solution, with cartesian product (by fabrizio_sanmar1): [Read More]

Padel tournament

Solutions A brute-force approach consists in enumerating all possible combination of pairs and check if any contains all pairs with the same sum. Enumerating and checking all the combinations is clearly expensive. Actually, there is only one possible combination that would satisfy the constraint and can be found directly. Intuitively, suppose we are 4 people and we want to play together in pairs. To make the match balanced, we have to join the strongest with the weakest. [Read More]

Triple Sum

Solutions Consider this test case: Visually, for each element of B we should mark the elements in A and C that are less than or equal to that element: And, for 3: Performing the operation on the second 3 is redundant, because triplets must be unique. Along the way, we could multiply the count of A’s marked elements with the count of B’s marked elements. All the results will be then accumulated. [Read More]