replace a couple raw loops with std::minmax_element algorithm