[C++] [O(n)] ✅ Simple greedy choices with detailed explanation.
\n# Code\n\ntypedef long long int ll;\nclass Solution {\npublic:\n    long long minimumMoves(vector<int>& nums, ll k, int maxChanges) {\n        int n = nums.si
\n# Code\n```\ntypedef long long int ll;\nclass Solution {\npublic:\n    long long minimumMoves(vector<int>& nums, ll k, int maxChanges) {\n        int n = nums.size();\n\n        //create next 1 ind array\n        vector<int> next1Ind(n, -1);\n        int last1SeenAt = -1;\n        for (int i = n - 1; i >= 0; i--) {\n            next1Ind[i] = last1SeenAt;\n            \n            if (nums[i] == 1) {\n                last1SeenAt = i;\n            }\n        }\n\n        if (last1SeenAt == -1) {\n            return k * 2;\n        }\n\n        int startInd = 0;\n        if (nums[0] != 1) {\n            startInd = next1Ind[0];\n        }\n\n        // First choice of 1 -> if dyLanIndex is chosen as 1 (can be done without taking time)\n        // Second choice of 1 -> the adjacent 1\'s can be consumed via step 2 using 1 operation\n        // Third choice of 1 -> using step 1 , create 1 at adjacent index and consume via step 2 (takes 2 ops)\n        // Other choice -> Use step 2 to bring 1\'s from side close to consume\n        // First we try to see if we can reach K 1\'s using the first 3 choices, if we can that is the answer\n        // If we cannot, the idea is to only choose dylanIndexes as index with value as 1, keep a left and right ptr \n        // that combined points to the closest kLeft to the startInd\n        // kLeft = k - 1 - maxChanges (we just want to fetch the 1\'s left after maxing third choice)\n        ll leftPtr = startInd, rightPtr = startInd, ctrL = 0, ctrR = 0, sumIndL = 0, sumIndR = 0, kLeft = k - 1 - maxChanges, ans = (ll)(1e12);\n\n        while (startInd != -1) {\n            // This runs just once, this is to set the right pointer so that closest 1\'s are covered\n            while (kLeft > 0) {\n                rightPtr = next1Ind[rightPtr];\n                ctrR ++;\n                sumIndR += rightPtr;\n                kLeft --;\n            }\n\n            int ctrFirstOps = ((startInd > 0 && nums[startInd - 1] == 1) ? 1 : 0) + ((startInd < (n - 1) && nums[startInd + 1] == 1) ? 1 : 0);\n            // See if we can get our 1\'s using 1st 3 choices\n            if (ctrFirstOps + maxChanges >= k - 1) {\n                if (ctrFirstOps >= k - 1) {\n                    ans = min(ans, k - 1);\n                } else {\n                    ans = min(ans, ctrFirstOps + (k - 1 - ctrFirstOps) * 2);\n                }\n            } \n            // Go back to the second option, bring 1\'s closer\n            else {\n                // Mantain the pointers, choose the closer 1\'s remove farther 1\'s\n                while (next1Ind[rightPtr] != -1 && (startInd - leftPtr > next1Ind[rightPtr] - startInd)) {\n                    rightPtr = next1Ind[rightPtr];\n                    ctrR ++;\n                    sumIndR += rightPtr;\n\n                    sumIndL -= leftPtr;\n                    ctrL --;\n                    leftPtr = next1Ind[leftPtr];\n                }\n\n                ans = min(ans, maxChanges * 2 + sumIndR - ctrR * startInd + ctrL * startInd - sumIndL);\n            }\n            \n            // Move startInd to the next 1\'s index\n            sumIndL += startInd;\n            ctrL ++;\n\n            sumIndR -= next1Ind[startInd];\n            ctrR --;\n\n            startInd = next1Ind[startInd];\n        }\n\n        return ans;\n    }\n};\n```
['Sliding Window', 'C++']