// Podany kod działa niepoprawnie dla pewnych danych. Zmień go tak, żeby działał
/// zawsze. Zastosuj reguły borrow checker'a znane z Rust'a i/lub metody synchronizacji.

#include <iostream>
#include <algorithm>
#include <numeric>
#include <thread>
#include <vector>

double median(std::vector<double> &values) {
    auto middle = values.begin() + values.size() / 2;
    std::nth_element(values.begin(), middle, values.end());
    return *middle;
}

double mean(const std::vector<double> &values) {
    return std::accumulate(values.begin(), values.end(), 0) / values.size();
}

std::pair<double, double> mean_and_median(std::vector<double> &values) {
    double x, y;

    std::thread t1([&x, &values]() { x = mean(values); });
    std::thread t2([&y, &values]() { y = median(values); });
    t2.join();
    t1.join();

    return std::make_pair(x, y);
}

int main() {
    std::vector<double> values = {8, 12, 9, 1, 100, -3};
    auto mean_median = mean_and_median(values);
    std::cout << mean_median.first << " " << mean_median.second << std::endl;
    return 0;
}
