C++ siding window
\tclass Solution {\n\tpublic:\n\t\tint minMoves(vector& nums, int k) {\n\t\t\tif(k == 1) {return 0;}\n\t\t\tvector ind;\n\t\t\tfor(int i = 0; i < nums.size(); i
\tclass Solution {\n\tpublic:\n\t\tint minMoves(vector<int>& nums, int k) {\n\t\t\tif(k == 1) {return 0;}\n\t\t\tvector<int> ind;\n\t\t\tfor(int i = 0; i < nums.size(); i++){\n\t\t\t\tif(nums[i] == 1) {\n\t\t\t\t\tind.push_back(i);\n\t\t\t\t}\n\t\t\t}\n\t\t\tint n = ind.size();\n\t\t\t\n\t\t\tint left = 0, right = k - 1;\n\t\t\tint min = (left + right) / 2;\n\t\t\tint res = 0;\n\t\t\tfor(int i = left; i <= right; i++){\n\t\t\t\tres += abs(ind[min] - (min - i) - ind[i]);\n\t\t\t}\n\t\t\t\n\t\t\tint ans = res;\n\t\t\twhile(right < n - 1){\n\t\t\t\tres -= (ind[min] - ind[left]);\n\t\t\t\tif(k % 2 == 0){\n\t\t\t\t\tres -= ind[min + 1] - ind[min];\n\t\t\t\t}\n\t\t\t\tres += ind[right + 1] - ind[min + 1];\n\t\t\t\tif(res < ans)  {ans = res;}\n\t\t\t\tleft++;\n\t\t\t\tright++;\n\t\t\t\tmin++;\n\t\t\t}\n\t\t\treturn ans;\n\t\t}\n\t};\n\n
[]