C++ || SLIDING WINDOW || PREFIX SUM || GREEDY
\nclass Solution {\npublic:\n    \n    typedef long long ll;\n    \n    int minMoves(vector<int>& nums, int k) {\n        \n       ll n=nums.size();\n        \n
```\nclass Solution {\npublic:\n    \n    typedef long long ll;\n    \n    int minMoves(vector<int>& nums, int k) {\n        \n       ll n=nums.size();\n        \n        vector<ll>A;\n        \n        for(int i=0;i<n;i++){\n            if(nums[i]==1){\n                A.push_back((ll)i);\n            }\n        }\n        \n        n=A.size();\n        \n        if(n<=1 || k==1){\n            return 0;\n        }\n        \n        vector<ll>prefix(n,0);\n        \n        prefix[0]=A[0];\n        \n        for(ll i=1;i<n;i++){\n            prefix[i]=prefix[i-1]+A[i];\n        }\n        \n        ll ans=1e18;\n        \n        ll subtract=0;\n        \n        if(k%2!=0){\n            \n            subtract=(k/2)*((k/2)+1);\n        \n        }\n        \n        else{\n            \n            ll a=(k/2)*((k/2)+1);\n            a/=2;\n            \n            ll b=((k/2)-1)*(k/2);\n            b/=2;\n            \n            subtract=(a+b);\n            \n        }\n        \n     \n        for(ll i=0;i<=n-k;i++){\n            \n            ll right=0;\n            ll left=0;\n            \n            if(k%2==0){\n                \n                right = prefix[i+k-1] - prefix[i+(k/2)] + A[i+(k/2)];\n                \n                left  = prefix[i+(k/2)-1] - (i-1>=0 ? prefix[i-1] : 0);\n                \n            }\n            \n            \n            else{\n                \n                right = prefix[i+k-1] - prefix[i+(k/2)];\n                \n                left  = prefix[i+(k/2)-1] - (i-1>=0 ? prefix[i-1] : 0);\n                \n            }\n            \n//             ll right=prefix[i+k-1]-prefix[i+((k+1)/2)-1];\n//             ll left =prefix[i+((k-1)/2)-1]-(i-1>=0 ? prefix[i-1] : 0);\n            \n            ans=min(ans,right-left-subtract);\n            \n           // cout<<left<<" "<<right<<" "<<subtract<<endl;\n            \n        }\n        \n        return ans;\n        \n    }\n};\n```
['Greedy', 'Sliding Window', 'Prefix Sum']