My Solution
\n/**\n * Time Complexity: O(n)\n * Space Complexity: O(n)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n public:\n  long long minimu
```\n/**\n * Time Complexity: O(n)\n * Space Complexity: O(n)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n public:\n  long long minimumMoves(const vector<int> &nums, const int k, const int maxChanges) {\n    constexpr int one = 1;\n    constexpr int upper_consecutive_ones = 3;\n    const int n = static_cast<int>(nums.size());\n    vector<int> one_indices;\n    int max_consecutive_ones = 0;\n    for (int consecutive_ones = 0, i = 0; i < n; ++i) {\n      if (nums[i] == one) {\n        one_indices.emplace_back(i);\n        max_consecutive_ones = max(max_consecutive_ones, ++consecutive_ones);\n      } else {\n        consecutive_ones = 0;\n      }\n    }\n    \n    max_consecutive_ones = min(max_consecutive_ones, upper_consecutive_ones);\n    if (maxChanges >= k - max_consecutive_ones) {\n      return 2LL * max(0, k - max_consecutive_ones) + max(0, min(k, max_consecutive_ones) - 1);\n    }\n    \n    long long ret = numeric_limits<long long>::max();\n    const int sliding_window_length = k - maxChanges;\n    const int n_ones = static_cast<int>(one_indices.size());\n    long long presums[n_ones + 1];\n    memset(presums, 0, sizeof(presums));\n    for (int i = 0; i < n_ones; ++i) {\n      presums[i + 1] = presums[i] + one_indices[i];\n    }\n    for (int start = 0; start + sliding_window_length - 1 < n_ones; ++start) {\n      const int end = start + sliding_window_length - 1;\n      const int mid = start + ((end - start) >> 1);\n      const long long left = static_cast<long long>(mid - start + 1) * one_indices[mid] - (presums[mid + 1] - presums[start]);\n      const long long right = (presums[end + 1] - presums[mid]) - static_cast<long long>(end - mid + 1) * one_indices[mid];\n      ret = min(ret, left + right);\n    }\n    return ret + (maxChanges << 1);\n  }\n};\n```
[]