Java Solution with Explanation
\n\nclass Solution {\n    \n    // Sliding window + prefix Sum\n\t\n\t// // Explanation with image :  https://leetcode.com/problems/minimum-adjacent-swaps-for-k
```\n\nclass Solution {\n    \n    // Sliding window + prefix Sum\n\t\n\t// // Explanation with image :  https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/987607/O(n)-explanation-with-picture\n    \n    /*\n    \n    1. First record the positions of 1\'s in an Array or list . Because that is what we need to calculate .\n    2. Then our target is to move all the k size 1s in together .\n    \n    Like for the below case ,\n    \n    nums = [1,0,0,1,0,1], k = 2 --> [0,3,5] --> this will record the 1\'s position . Now we have to group together 2 count of 1\'s .\n    \n    So we will do a sliding window of size k in [0,3,5] . when j-i+1 == k , we will calculate the number of moves .\n    \n    Now take a bigger example , suppose record array is something like below ,\n    \n     0 1 2 3 4 5  6  7\n    [0,2,4,6,7,9,10,11] and k is 5 .\n    \n    Now the next question is , how we can calculate the move count for 0 --> 4 in pos array i.e. for 0,2,4,6,7\n    \n    so , here the idea will be , try to move everything to the center , i.e move everything close to 4 and calculate the count . Hence below will be total count ,\n    \n       0  ,   2   , 4 ,   6   ,   7\n    (4-0) + (4-2) + 0 + (6-4) + (7-4) ==> -(0+2) + (6+7) ==> i.e -(left_side_sum) + (right_side_sum) as 4 will get canceled at both side . \n    left_side_sum  and  right_side_sum can be achived by prefix_sum in O(1) .\n    \n    for Odd nomber of window , number of 4 in right and left side will cancel each other .\n    But for even number of window , we will have 1 extra 4 , so that we need to consider .\n    \n    Now once we got this min value calculated , this wont be the final answer .\n    Because here we have calculated the move count if all 1\'s can be moved to pos 4 . But that is not the case .\n    \n    Like 2 will move to 3 , not 4 and 0 --> 2 , 6--> 5 , 7--> 6 \n    [2,3,4,5,6] --> this will be the final pos array of 1\'s .\n    \n    Here you can see the first radious of 1\'s i.e 2 and 6 , reduced by 1 , second radious is reduced by 2 and so on .\n    Here also we have to consider the odd and even case . as for odd , in one side the radious will be greater .\n    \n    */\n    \n    public int minMoves(int[] a, int k) {\n        \n        int n = a.length;\n\t\tArrayList<Integer> oneInd = new ArrayList<>();\n        \n        // Create Pos array\n\t\tfor (int i = 0; i < n; i++){\n            if (a[i] == 1)\n\t\t\t\toneInd.add(i);\n        }\n        \n\t\tint prfsm[] = new int[oneInd.size()];\n        \n        // Create the Prefix_sum\n\t\tfor (int i = 0; i < oneInd.size(); i++)\n\t\t\tprfsm[i] += oneInd.get(i) + (i == 0 ? 0 : prfsm[i - 1]);\n        \n        \n\t\tint ans = Integer.MAX_VALUE;\n\t\tint left = 0, right = k - 1;\n        \n        // Loop over oneInd pos array\n\t\twhile (right < oneInd.size()) {\n            \n            // For Odd case\n\t\t\tif (k % 2 == 1) {\n                // get the median index\n\t\t\t\tint medianInd = left + (k + 1) / 2 - 1;\n                \n                // get the radious\n\t\t\t\tint rad = medianInd - left ;\n                \n\t\t\t\tint res = (prfsm[right] - prfsm[medianInd])\n\t\t\t\t\t\t- ((medianInd == 0 ? 0 : prfsm[medianInd - 1]) - (left == 0 ? 0 : prfsm[left - 1])) - rad * (rad + 1);\n                \n\t\t\t\tans = Math.min(ans, res);\n\t\t\t} \n            \n            // For even case\n            else {\n\t\t\t\tint medianInd = left + (k + 1) / 2 - 1;\n\t\t\t\tint rad = medianInd - left;\n\t\t\t\tint res = (prfsm[right] - prfsm[medianInd])\n\t\t\t\t\t\t- ((medianInd == 0 ? 0 : prfsm[medianInd - 1]) - (left == 0 ? 0 : prfsm[left - 1]))\n\t\t\t\t\t\t- oneInd.get(medianInd) - rad * (rad + 1) - (rad + 1);\n\t\t\t\tans = Math.min(ans, res);\n\t\t\t}\n            \n\t\t\tleft++;\n\t\t\tright++;\n\t\t}\n        \n\t\treturn ans;\n      \n    }\n}\n\n```
[]