Returning a pair
Completed
Write pair<int, int> minMax(const vector<int> &v). Read a list and print its minimum and maximum using structured bindings: auto [lo, hi] = minMax(v);
Input
Line 1: n. Line 2: n integers.
Output
One line: min max
Example
Input
5 3 9 -2 7 1
Output
-2 9
Editor
C++
Hints
return {lo, hi}; builds the pair.
Structured bindings (C++17) unpack it into two names.
Solution explanation
pair and tuple are the C++ way to return several values.
Show solution
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
pair<int, int> minMax(const vector<int> &v) {
int lo = v[0], hi = v[0];
for (int x : v) {
if (x < lo) lo = x;
if (x > hi) hi = x;
}
return {lo, hi};
}
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int &x : v) cin >> x;
auto [lo, hi] = minMax(v);
cout << lo << " " << hi << "\n";
return 0;
}