C++ sliding window, beats 98% in time, 93% in space
Complexity : T=O(n), S=O(n)\nSingle Pass\n\nKeep count of zeros on left of window mid in a queue(qLeft), and their sum in leftSum.\nKeep count of zeros on right
Complexity : T=O(n), S=O(n)\nSingle Pass\n\nKeep count of zeros on left of window mid in a queue(qLeft), and their sum in leftSum.\nKeep count of zeros on right of window mid in a queue(qRight), and their sum in rightSum.\nWhenever window slides, (new windowSum)=(old windowSum)-(old leftSum)+(new rightSum)\nKeep updating leftSum and rightSum using the queues.\n\n```\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int k)\n    {\n        int left=0,right=0,leftCount=k/2,rightCount=k/2,count=0;\n        long long int leftSum=0,rightSum=0,windowSum=0,ans=INT_MAX;\n        queue<int>qleft,qright;\n        if(k%2==0)\n            rightCount--;\n        while(right<nums.size() && nums[right]==0)//skipping initial zeros\n            right++;\n        if(right<nums.size())//found a 1\n            right++;\n        while(right<nums.size() && count<leftCount)\n        {\n            int zeros=0;\n            while(right<nums.size() && nums[right]==0)\n            {\n                zeros++;\n                right++;\n            }\n            if(right<nums.size())//found a 1\n            {\n                count++;\n                qleft.push(zeros);\n                leftSum+=zeros;\n                windowSum+=(count*zeros); \n                right++;\n            }\n        }//first half window found\n        count=rightCount;\n        while(right<nums.size() && count)\n        {\n            int zeros=0;\n            while(right<nums.size() && nums[right]==0)\n            {\n                zeros++;\n                right++;\n            }\n            if(right<nums.size())\n            {\n                rightSum+=zeros;\n                windowSum+=(count*zeros);\n                qright.push(zeros);\n                count--;\n                right++;\n            }\n        }//first window found\n        ans=min(ans,windowSum);\n        while(right<nums.size())\n        {\n            int zeros=0;\n            while(right<nums.size() && nums[right]==0)\n            {\n                zeros++;\n                right++;\n            }\n            if(right<nums.size())//found a 1\n            {\n                windowSum-=leftSum;\n                if(!qleft.empty())\n                {\n                    int leftMost=qleft.front();\n                    qleft.pop();\n                    leftSum-=leftMost;\n                }\n                if(!qright.empty())\n                {\n                    int leftMostOnRight=qright.front();\n                    qright.pop();\n                    qleft.push(leftMostOnRight);\n                    leftSum+=leftMostOnRight;\n                    rightSum-=leftMostOnRight; \n                    if(k%2==0 && k>2)\n                        windowSum+=leftMostOnRight;\n                }  \n                qright.push(zeros);\n                rightSum+=zeros;\n                windowSum+=rightSum;\n                ans=min(ans,windowSum);\n                right++;\n            }\n        }\n        return ans;\n    }\n};\n```
[]