C# solution: Attempt and understand Leetcode 462 first
Intuition\n Describe your first thoughts on how to solve this problem. \nI strongly recommend one should attempt and go back to 462 first. This problem essentia
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI strongly recommend one should attempt and go back to 462 first. This problem essentially is to run the 462 algorithm on all k-subarrays of the indices array ($$nums[i] == 1$$)\n# Approach\n<!-- Describe your approach to solving the problem. -->\nRefer to the comments. The odd and even scenarios are nicely combined into one by floor division.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code\n```\n/*\n * @lc app=leetcode id=1703 lang=csharp\n *\n * [1703] Minimum Adjacent Swaps for K Consecutive Ones\n */\n\n// @lc code=start\nusing System.Linq;\nusing System.Collections.Generic;\nusing System;\npublic class Solution {\n    /// <summary>\n    ///     Remember one key point: the median of a sequence is where we should swap all sequence elements to if we want to make all elements equal and the number of +- 1 swaps is minimized (Leetcode 462). <br/>\n    ///     So we record occurence of 1 in nums. Then we consider length k subarrays of the indices array and decide how many swaps to create a consecutive k 1s around the median. Leetcode 462 tells us how many swaps to turn all of them to the median. To get k consecutives 1s around mid: <br/>\n    ///     1. if k is odd, number of moves = 2 * (1 + 2 + ... + (k - 1) // 2) = k // 2 * (k // 2 + 1) <br/>\n    ///     2. if k is even, number of moves = (1 + 2 + ... + k // 2) + (1 + 2 + ... + (k - 2) // 2) = (k // 2) * (k // 2) <br/>\n    ///     Since k can be large, using the 462 algorithm can be time consuming O(nk). If we look carefully at the formula, minMoves2(nums) = nums[-1] - nums[0] + nums[-2] - nums[1] +... : <br/>\n    ///     1. if n is odd: minMoves(nums) = (nums[mid + 1] +... + nums[-1]) - (nums[mid - 1] + nums[mid - 2] + ... + nums[0]) <br/>\n    ///     2. if n is even: minMoves(nums) = (nums[mid] +... + nums[-1]) - (nums[mid - 1] + nums[mid - 2] + ... + nums[0]) <br/>\n    ///     We can prepare prefix sum array to quickly calculate the output of Leetcode 462\n    /// </summary>\n    /// <param name="nums"></param>\n    /// <param name="k"></param>\n    /// <returns></returns>\n    public int MinMoves(int[] nums, int k) \n    {\n        List<int> prefix = new List<int>();\n        prefix.Add(0);\n        for (int i = 0; i < nums.Length; i++)\n        {\n            if (nums[i] == 1)\n            {\n                prefix.Add(prefix.Last() + i);\n            }\n        }\n        int result = Int32.MaxValue;\n        for (int i = 0; i < prefix.Count - k; i++)\n        {\n            result = Math.Min(result, prefix[i + k] + prefix[i] - prefix[i + (k + 1) / 2] - prefix[i + k / 2] - (k / 2)*((k + 1) / 2));\n        }\n        return result;\n    }\n}\n// @lc code=end\n\n\n```
['C#']