[Java] soluton
Idea :\n1.We want to group K 1 together. \n2.Store all 1\'s index first\n3. k-sliding window on those one\'s index. \n4.The min cost is if we group them togethe
Idea :\n1.We want to group K 1 together. \n2.Store all 1\'s index first\n3. k-sliding window on those one\'s index. \n4.The min cost is if we group them together base on the median index and expand it (There are two cases we treat them independently. If K is odd, we only have 1 median index. If K is even,we have 2 median indexs)\n\n```\nclass Solution {\n    long res=Integer.MAX_VALUE;\n    public int minMoves(int[] nums, int k) {\n        long sum=0;\n        Queue<Integer>q=new LinkedList<>();\n        List<Integer>A=new ArrayList<>();\n       \n        for(int i=0;i<nums.length;i++){\n            if(nums[i]==1)A.add(i);\n        }\n        long pre[]=new long[A.size()];\n        for(int i=0;i<A.size();i++){\n            sum+=A.get(i);\n            pre[i]=sum;\n        }\n    \n        \n        for(int i=0;i<A.size();i++){\n            q.add(i);\n            if(q.size()<k)continue;\n            \n            if(k%2==0){\n                int half=k/2-1;\n                int mid2=A.get(k/2+q.peek());\n                int mid1=A.get(k/2-1+q.peek());\n                \n                long l1=mid1-half;\n                long r1=mid1+half+1;\n                long a1=(l1+mid1-1)*(half)/2;\n                long b1=(mid1+1+r1)*(half+1)/2;\n                long p1=psum(pre,q.peek(),k/2-1+q.peek()-1);\n                long p2=psum(pre,k/2-1+q.peek()+1,i);\n                res=Math.min(res,a1-p1+p2-b1);\n                \n                \n                long l2=mid2-half-1;\n                long r2=mid2+half;\n                long a2=(l2+mid2-1)*(half+1)/2;\n                long b2=(mid2+1+r2)*(half)/2;\n                long p3=psum(pre,q.peek(),k/2+q.peek()-1);\n                long p4=psum(pre,k/2+q.peek()+1,i);\n                res=Math.min(res,a2-p3+p4-b2);\n                \n            }\n            else{\n                long mid=A.get((k/2)+q.peek());\n                long l=mid-k/2;\n                long r=mid+k/2;\n                long a=(l+mid-1)*(k/2)/2;\n                long b=(mid+1+r)*(k/2)/2;\n                long p1=psum(pre,q.peek(),(k/2)+q.peek()-1);\n                long p2=psum(pre,(k/2)+q.peek()+1,i);\n                res=Math.min(res,a-p1+p2-b); \n            }\n            q.poll();\n        }\n        if(res==Integer.MAX_VALUE)return 0;\n        return (int)(res);\n    }\n    \n    \n    \n    public long psum(long A[],int i,int j){\n        if(j<0)return 0;\n        if(i==0)return A[j];\n        return A[j]-A[i-1];\n    }\n}\n```
[]