[JAVA] O(n) sliding window Solution.
Explaination with pictures: https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/987607/O(n)-explanation-with-picture\n\n\nclass 
Explaination with pictures: https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/987607/O(n)-explanation-with-picture\n\n```\nclass Solution {\n    public int minMoves(int[] a, int k) {\n\t\tint n = a.length;\n\t\tArrayList<Integer> oneInd = new ArrayList<>();\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif (a[i] == 1)\n\t\t\t\toneInd.add(i);\n\t\tint prfsm[] = new int[oneInd.size()];\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\t\tint ans = Integer.MAX_VALUE;\n\t\tint left = 0, right = k - 1;\n\t\twhile (right < oneInd.size()) {\n\t\t\tif (k % 2 == 1) {\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])) - rad * (rad + 1);\n\t\t\t\tans = Math.min(ans, res);\n\t\t\t} 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\t\t\tleft++;\n\t\t\tright++;\n\t\t}\n\t\treturn ans;\n\t}\n}\n```
['Java']