C++ || PrefixSum || Sliding Window
\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int k) {\n        vector<int> ones;\n        for(int i=0;i<nums.size();i++)\n        {\n       
```\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int k) {\n        vector<int> ones;\n        for(int i=0;i<nums.size();i++)\n        {\n            if(nums[i] == 1)\n                ones.push_back(i);\n        }\n        \n        //Explore Each window of size k using sliding window of fixed length k.\n        // If k = 5 and have values [0,2,4,5,7]\n        //We need to change all values to median and again change it to consecutive.\n        //[0,2,4,5,7]  --->(Step 1)   [4,4,4,4,4]  ---->(Step 2) [2,3,4,5,6]\n        \n        //Step 1: Is simular to Minimum moves to make all array elements equal.\n        //(4-0) + (4-2) + (5-4) + (7 - 4) ===> (5 + 7) - (0 + 2) ==> \n        //Which is difference in sum of right and sum of left.\n        \n        //Step 2: Change [4,4,4,4,4] to consecutive [2,3,4,5,6]\n        //We use radius = k/2\n        //(k + k-1 + k-2 + ... + 0 + 1 + 2 + 3 +... + k-1 + k+1)\n        //(1 + 2 + 3 + .... + k) * 2 => k(k + 1)/2 * 2 ==> k(k+1)\n        //right - left - k(k+1) for k is odd\n        //right - left - i - k(k+1) - (k+1) for k is even\n         int n=ones.size();\n          vector<long long int> preSum(n, 0);\n         preSum[0] = ones[0];\n        for(int i=1;i<n;i++)\n            preSum[i] = preSum[i-1] + ones[i];\n        int i=0,j=0;\n       long long int ans  = LLONG_MAX;\n        while(j < n){\n            if(j-i+1 == k){\n                // cout<<"Hello"<<endl;\n                int mid = (j + i)/2;\n                long long int right = preSum[j] - preSum[mid];\n                long long int left = (mid-1 < 0 ? 0 : preSum[mid - 1]) - (i-1 < 0 ? 0 : preSum[i-1]);\n             // cout<<mid<<" "<<left<<" "<<right<<endl;\n                int radius = (k-1)/2;\n                long long int p = (long long int)radius*(radius + 1);\n                long long int sum = right - left - p;\n                if(k % 2 == 0){\n                    sum -= (radius + 1);\n                    sum -= ones[mid];\n                }\n               // cout<<sum<<endl;\n                ans = min(ans, sum);\n                i++;\n            }\n            j++;\n        }\n        return ans;\n    }\n};\n```
['C', 'Prefix Sum']