C++ solution with explanation reference | O(n) | sliding window | prefix sum
Intuition\n Describe your first thoughts on how to solve this problem. \nuse sliding window as we need k adjacent one - all element in continuous order\n\n# App
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nuse sliding window as we need k adjacent one - all element in continuous order\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- question seems vey hard to solve at the first sight\n-  i thought some extraordinary concepts or algo will require to solve this questoion\n-  but after watching hint and solution , i realised concept is well known but the logic reuire is extraordinary.\n-  it require one logic where **we take median of sorted array as pivot to get min move to gather all element at one point** .\n-  require sliding window concept in an awesome way.\n-  solution with better explanation - [https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/solutions/987607/o-n-explanation-with-picture/](https://)\n-  first bring all one at pivot point and then put all element contiously in adjacent side .\n-  code is done using above explanation pic.\n- i am attaching pic of that above link here, \n![aPSNTZybtBFzHqW.png](https://assets.leetcode.com/users/images/3a497c07-d1f9-4b37-8628-e130996fb64d_1666269752.0851977.png)\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# Code\n```\n#define ll long long\n\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        \n        // store index of all one in sorted order\n        vector<int> index;\n        for(int i=0;i<n;i++){\n            if(nums[i]){\n                index.push_back(i);\n            }\n        }\n\n        // calculate prefix sum of index array\n        vector<ll> prefix;\n        ll sum = 0;\n        for(int x : index){\n            sum += x;\n            prefix.push_back(sum);\n        }\n\n        // now take window of k size consecutively\n        int i=0,j=k-1;\n        ll radius = (k%2 ? k/2 : (k-2)/2);\n        ll ans = 1e15;\n        while(j<index.size()){\n            ll temp = 0;\n            if(k%2){\n                int pivot = (i+j)/2;\n                temp = prefix[j]-prefix[pivot] -\n                        ((pivot>0 ? prefix[pivot-1] : 0)-(i>0 ? prefix[i-1] : 0)) -\n                        radius*(radius+1);\n            }\n            else{\n                int pivot = (i+j)/2;\n                temp = prefix[j]-prefix[pivot] -\n                        ((pivot>0 ? prefix[pivot-1] : 0)-(i>0 ? prefix[i-1] : 0)) - index[pivot] -\n                        radius*(radius+1) - (radius+1);\n            }\n            ans = min(ans, temp);\n            i++;j++;\n        }\n\n        return ans;\n    }\n};\n```
['Sliding Window', 'Prefix Sum', 'C++']