greedy + sld wind + pefix sum solution 82% faster O(n)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# java\n```\nclass Solution {\n    public int minMoves(int[] arr, int k) {\n        int i=0;\n        int j=0;\n        int n=arr.length;\n        int count=0;\n        long pre[]=new long[n];;\n        int ind=0;\n        int st=0;\n        int mid=k/2;\n        int radius=mid;\n        long ans=Integer.MAX_VALUE;\n        while(j<n && count<k-1){\n            if(arr[j]==1){\n                count++;\n                pre[count-1]=j+((count>1)?pre[count-2]:0);\n            }\n            j++;\n        }\n        \n        while(j<n){\n            while(j<n && arr[j]==0){\n                j++;\n            }\n            if(j==n) break;\n            count++;\n            pre[st+k-1]=j+((k>1)?pre[st+k-2]:0);\n            j++;\n            if((k&1)==1){\n                long left_sum=0+((mid>0)?pre[st+mid-1]:0);\n                long right_sum=pre[st+k-1]-pre[st+mid];\n                left_sum-=(st>0)?pre[st-1]:0;\n                long t=radius*(radius+1);\n                ans=Math.min(ans,right_sum-left_sum-t);\n            }\n            else{\n                long left_sum=0+((mid>0)?pre[st+mid-1]:0);\n                long right_sum=pre[st+k-1]-pre[st+mid];\n                long x=pre[st+mid]-((mid>0)?pre[st+mid-1]:0);\n                left_sum-=(st>0)?pre[st-1]:0;\n                long t=radius*radius;\n                ans=Math.min(ans,right_sum-left_sum-t+x);\n                left_sum-=x;\n                right_sum+=x;\n                ans=Math.min(ans,right_sum-left_sum-t-x);\n            }\n            i++;\n            count--;\n            st++;\n            while(i<n && arr[i]==0) i++;\n        }\n        return (int)ans;\n    }\n}\n```\n# C++\n```\nclass Solution {\npublic:\n    int minMoves(vector<int>& arr, int k) {\n        int i=0;\n        int j=0;\n        int n=arr.size();\n        int count=0;\n        vector<long> pre(n);\n        int ind=0;\n        int st=0;\n        int mid=k/2;\n        int radius=mid;\n        int ans=INT_MAX;\n        while(j<n && count<k-1){\n            if(arr[j]==1){\n                count++;\n                pre[count-1]=j+((count>1)?pre[count-2]:0);\n            }\n            j++;\n        }\n        \n        while(j<n){\n            while(j<n && arr[j]==0){\n                j++;\n            }\n            if(j==n) break;\n            count++;\n            pre[st+k-1]=j+((k>1)?pre[st+k-2]:0);\n            j++;\n            if(k&1){\n                long left_sum=0+((mid>0)?pre[st+mid-1]:0);\n                long right_sum=pre[st+k-1]-pre[st+mid];\n                left_sum-=(st>0)?pre[st-1]:0;\n                long t=radius*(radius+1);\n                ans=min(1L*ans,right_sum-left_sum-t);\n            }\n            else{\n                long left_sum=0+((mid>0)?pre[st+mid-1]:0);\n                long right_sum=pre[st+k-1]-pre[st+mid];\n                int x=pre[st+mid]-((mid>0)?pre[st+mid-1]:0);\n                left_sum-=(st>0)?pre[st-1]:0;\n                long t=radius*radius;\n                ans=min(1L*ans,right_sum-left_sum-t+x);\n                left_sum-=x;\n                right_sum+=x;\n                ans=min(1L*ans,right_sum-left_sum-t-x);\n            }\n            i++;\n            count--;\n            st++;\n            while(i<n && arr[i]==0) i++;\n        }\n        return ans;\n    }\n};\n\n\n```
['C++', 'Java']