C++ O(n) sln
```\nint minMoves(vector& nums, int k) {\n        using ll = long long;\n        const int n = size(nums);\n        vector positions;\n        for (int i = 0; i
```\nint minMoves(vector<int>& nums, int k) {\n        using ll = long long;\n        const int n = size(nums);\n        vector<ll> positions;\n        for (int i = 0; i < n; ++i) {\n            if (nums[i]) {\n                positions.push_back(i);\n            }\n        }\n        vector<ll> sums(size(positions) + 1);\n        partial_sum(begin(positions), end(positions), begin(sums) + 1);\n        ll ans = numeric_limits<int>::max();\n        const auto getSum = [&](int l, int r) {\n            if (l > r) return 0ll;\n            return sums[r + 1] - sums[l];\n        };\n        const auto isEven = k % 2 == 0;\n        const ll r = (k - 1) / 2;\n        for (int i = k - 1; i < size(positions); ++i) {\n            const ll center = i - r;\n            ll res = getSum(center + 1, i) - getSum(center - r - isEven, center - 1);\n            if (isEven) {\n                res -= (-positions[center] + r + 1);\n            }\n            ans = min(ans, res);\n        }\n        return ans - r * (r + 1);\n    }
[]