I rewrote it using STL functions so its easier to reason about ```cpp double dist(std::array a, std::array b){ //ALG 1 //Get point average position of a and b auto avg1 = std::accumulate(a.begin(), a.end(), vector(0,0)) / a.size(); // This could overflow easier than dividing each summand but Im lazy auto avg2 = std::accumulate(b.begin(), b.end(), vector(0,0)) / b.size(); //Vector connecting two average points and normalize it to calculate dot product. vector line=avg2-avg1; line = line.normalize(); // Line starts at avg1 to avg2 first we find the vector thats furthest along that line in a auto tmpmax2 = *std::ranges::max_element(a, {}, [&](auto pt){return vector::dotProduct(pt-avg1, line)},); // flip the line and swap the origin to and find the furthest point for b auto tmpmax1 = *std::ranges::max_element(b, {}, [&](auto pt){return vector::dotProduct(pt-avg2, -line)},); return (tmpmax2-tmpmax1).norm(); }, ```
```diff -auto avg1 = std::accumulate(a.begin(), a.end(), vector(0,0)) / a.size(); +auto avg1 = std::ranges::fold_left(a, vector(0,0), std::plus{}, [](auto&& elem) { return elem / a.size(); },); ```