C
Runtime: 315 ms, faster than 100.00% of C online submissions for Minimum Adjacent Swaps for K Consecutive Ones.\nMemory Usage: 23 MB, less than 100.00% of C onl
Runtime: 315 ms, faster than 100.00% of C online submissions for Minimum Adjacent Swaps for K Consecutive Ones.\nMemory Usage: 23 MB, less than 100.00% of C online submissions for Minimum Adjacent Swaps for K Consecutive Ones.\n```\nint minMoves(int* nums, int numsSize, int k){\n    int n = numsSize;   \n    if(k == 1)\n        return 0;\n    int* OnePos = malloc(n * sizeof(int));\n    int Cn = 0;\n    for(int i = 0; i < n; i++){\n        if(nums[i] == 1){\n            OnePos[Cn] = i;\n            Cn++;\n        }\n    }\n    \n    int min = INT_MAX;\n    long long* dp = malloc((Cn+1) * sizeof(long long));\n    dp[0] = 0;\n    for(int i = 1; i <= Cn; i++){\n        dp[i] = dp[i-1] + OnePos[i-1];\n    }\n    for(int i = 0; i <= Cn - k; i++){\n        int mid = i + k/2 ;\n        int cur = 0;\n        long long sum = 0;\n        sum = ( (long long)OnePos[mid] + OnePos[mid]-k/2 )*(k/2 + 1)/2 ;        \n        cur = sum- (dp[mid+1] - dp[i]) ;\n \n        if(k > 2){\n            int r = k - k/2;\n            sum = ((long long)2*OnePos[mid] + r-1) * r/2;\n            cur += dp[i+k] - dp[mid]  - sum;\n        }\n        min = fmin(min , cur);\n        if(min == 0)\n            return 0;\n    }   \n    return min;\n}\n```
['Dynamic Programming']