min_element & max_element
Completed
Read n integers and print the minimum, its index, the maximum and its index (first occurrence).
Input
Line 1: n. Line 2: n integers.
Output
Two lines: min index, max index.
Example
Input
6 4 -2 9 -2 9 0
Output
-2 1 9 2
Editor
C++
Hints
These return iterators; subtract begin() to get the index.
Solution explanation
Iterator arithmetic converts positions to indexes.
Show solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int &x : v) cin >> x;
auto mn = min_element(v.begin(), v.end());
auto mx = max_element(v.begin(), v.end());
cout << *mn << " " << mn - v.begin() << "\n" << *mx << " " << mx - v.begin() << "\n";
return 0;
}