#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <cmath>
template<typename Iter>
void show_max_difference(Iter beg, Iter end)
{
// calculate the absolute differences
std::vector<double> diffs;
std::adjacent_difference(beg, end, back_inserter(diffs), [](double x, double y){return std::abs(x-y);});
// find the largest one
std::vector<double>::iterator it = std::max_element(diffs.begin()+1, diffs.end());
size_t idx = std::distance(diffs.begin(), it);
std::cout << "The largest difference is " << *it << " between " << beg[idx-1] << " and " << beg[idx] << '\n';
}
int main()
{
double a[9] = {1.93, -4.68, -5.05, -8.08, -2.95, -0.43, 7.03, -7.20, 9.86};
show_max_difference(a, a+9);
int a2[9] = {9, -4, -5, -8, -3, 0, 7, -7, 9};
show_max_difference(a2, a2+9);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDxudW1lcmljPgojaW5jbHVkZSA8Y21hdGg+Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBJdGVyPgp2b2lkIHNob3dfbWF4X2RpZmZlcmVuY2UoSXRlciBiZWcsIEl0ZXIgZW5kKQp7CiAgICAvLyBjYWxjdWxhdGUgdGhlIGFic29sdXRlIGRpZmZlcmVuY2VzCiAgICBzdGQ6OnZlY3Rvcjxkb3VibGU+IGRpZmZzOwogICAgc3RkOjphZGphY2VudF9kaWZmZXJlbmNlKGJlZywgZW5kLCBiYWNrX2luc2VydGVyKGRpZmZzKSwgW10oZG91YmxlIHgsIGRvdWJsZSB5KXtyZXR1cm4gc3RkOjphYnMoeC15KTt9KTsKCiAgICAvLyBmaW5kIHRoZSBsYXJnZXN0IG9uZQogICAgc3RkOjp2ZWN0b3I8ZG91YmxlPjo6aXRlcmF0b3IgaXQgPSBzdGQ6Om1heF9lbGVtZW50KGRpZmZzLmJlZ2luKCkrMSwgZGlmZnMuZW5kKCkpOwogICAgc2l6ZV90IGl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