cpp slolution beats 99%
\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int k) {\n        if (k <= 1) {\n            return 0;\n        }\n        int n = nums.size();
```\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int k) {\n        if (k <= 1) {\n            return 0;\n        }\n        int n = nums.size();\n        int l = 0;\n        int count = 0;\n        for (int i = 0; i < n; ++ i) {\n            if (nums[i] == 1) {\n                ++ count;\n            }\n        }\n        vector<int> arr(count);\n        int index = 0;\n        for (int i = 0; i < n; ++ i) {\n            if (nums[i] == 1) {\n                arr[index ++] = i;\n            }\n        }\n        int m = (k - 1) / 2;\n        int pre = 0;\n        for (int i = 1; i <= m; ++ i) {\n            pre += (arr[i] - arr[i - 1]) * i;\n        }\n        int post = 0;\n        for (int i = m + 1; i < k; ++ i) {\n            post += (arr[i] - arr[i - 1]) * (k - i);\n        }\n        int extra = 0;\n        if (m > 0) {\n            extra += m * (m + 1) / 2;\n        }\n        if (k - m - 1 > 0) {\n            extra += (k - m - 1) * (k - m) / 2;\n        }\n        int res = pre + post - extra;\n        int r = res;\n        int half = (k - 1) / 2;\n        for (int i = k; i < index; ++ i) {\n            int m = i - (k - 1) + half;\n            int new_res = res;\n            if (half > 0) {\n                new_res += (arr[m] - arr[m - 1]) * (half + 1) - (arr[m] - arr[i - k]);\n            }\n            new_res += arr[i] - arr[m] - (arr[m] - arr[m - 1]) * (k - half - 1);\n            res = new_res;\n            r = min(r, res);\n        } \n        return r;\n    }\n}; \n```
[]