[JAVA] More Intuitive based on top voted solution
Idea is from the POST\nhttps://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/987607/O(n)-explanation-with-picture\n\nI re-wrote th
Idea is from the POST\nhttps://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/987607/O(n)-explanation-with-picture\n\nI re-wrote this which handles the odd and even case using same formula and just handling the extra element individually. \n\n```\nclass Solution {\n    public int minMoves(int[] nums, int k) {\n        List<Integer> list = new ArrayList();\n        int index = 0;\n        for(int num : nums){\n            if(num==1){\n                list.add(index);\n            }\n             index++;\n        }\n        int n = list.size();\n        int res = Integer.MAX_VALUE;\n        \n        int[] ps = new int[n+1];\n        \n        for(int i =1; i<=n; i++){\n            ps[i] = ps[i-1] + list.get(i-1);\n        }\n        if(k %2 == 0){\n            //even case\n            int r = (k-1)/2;\n            for(int i = r; i + r+1 < n; i++){\n                //Same as odd and even- can be move to a separate method\n                int left = Math.abs((ps[i] - ps[i-r] ) - list.get(i) *r);\n                int right = Math.abs((ps[i+1+r] - ps[i+1]) - list.get(i) *r);\n                int cost = left + right + Math.abs(list.get(i+r+1) - list.get(i));\n                int save = (r * (r+1)) + (r+1);\n                res = Math.min(res, cost - save);\n            }\n        }else{\n            int r = k/2;\n            for(int i = r; i + r < n; i++){\n                //Same as odd and even - can be move to a separate method\n                int left = Math.abs((ps[i] - ps[i-r] )  - list.get(i) *r);\n                int right = Math.abs((ps[i+1+r] - ps[i+1]) - list.get(i) *r);\n                int cost = left + right;\n                int save = r * (r+1);   \n                res = Math.min(res, cost - save);\n            }\n        }\n        \n        return res;\n    }\n}\n```
[]