C++ Tape Equilibrium
#include <cstdlib>
#include <limits>
#include <numeric>
#include <vector>
long long tapeEquilibrium(const std::vector<int>& a)
{
long long firstPart = 0;
long long secondPart = std::accumulate(a.begin(), a.end(), 0LL);
long long min = std::numeric_limits<long long>::max();
for (std::size_t i = 0; i + 1 < a.size(); ++i) {
// index `i` rather than `a[i]` here.
firstPart += static_cast<long long>(i);
secondPart -= static_cast<long long>(i);
long long difference = std::llabs(firstPart - secondPart);
min = std::min(min, difference);
}
return min;
}
This keeps left and right running sums and updates the smallest difference at each split point.