[Java] | Prefix Sum | O (n)
\nclass Solution {\n    public int minMoves(int[] nums, int k) {\n        if(k <= 1) return 0;\n        List<Integer> ones = new ArrayList<>();\n        ones.ad
```\nclass Solution {\n    public int minMoves(int[] nums, int k) {\n        if(k <= 1) return 0;\n        List<Integer> ones = new ArrayList<>();\n        ones.add(0);\n        for(int i = 0; i < nums.length; i++) {\n            if(nums[i] == 1) ones.add(i);\n        }\n        int[] prefix = new int[ones.size()];\n        for(int i = 1; i < prefix.length; i++) {\n            prefix[i] = prefix[i-1] + ones.get(i);\n        }\n        int ans = Integer.MAX_VALUE;\n        int radius = (k-1)/2;\n        for(int i = 1; i <= ones.size() - k; i++) {\n            /*\n            * Left Boundary -> i\n            * Right Boundary -> i + k - 1\n            * Mid Index = i + radius\n            */\n            int midIndex = i + radius;\n            int leftCost = prefix[midIndex-1] - prefix[i-1];\n            int rightCost = prefix[i + k - 1] - prefix[midIndex];\n            int totalCost = rightCost - leftCost - ((k % 2 == 0) ? ones.get(midIndex) : 0);\n            ans = Math.min(ans, totalCost);\n        }\n        ans -= radius*(radius+1);\n        if(k % 2 == 0) ans -= k/2;\n        return ans;\n    }\n}\n```
[]