Kotlin. Beats 100% (355 ms). Doubled array (System.arraycopy) + sliding window
\n\n# Code\n\nclass Solution {\n    fun minSwaps(nums: IntArray): Int {\n        var ones = 0\n        for (n in nums) {\n            ones += n\n        }\n\n  
![image.png](https://assets.leetcode.com/users/images/9c6c7d04-c607-4de3-84a2-16e1a8a81260_1722584171.8669941.png)\n\n# Code\n```\nclass Solution {\n    fun minSwaps(nums: IntArray): Int {\n        var ones = 0\n        for (n in nums) {\n            ones += n\n        }\n\n        var windowOnes = 0\n        for (i in 0 until ones) {\n            windowOnes += nums[i]\n        }\n        if (windowOnes == ones) return 0\n\n        var maxWindowOnes = windowOnes\n\n        val bigArray = IntArray(nums.size shl 1)\n        System.arraycopy(nums, 0, bigArray, 0, nums.size)\n        System.arraycopy(nums, 0, bigArray, nums.size, nums.size)\n\n        var left = 0\n        var right = ones\n        while (left < nums.size) {\n            windowOnes -= bigArray[left++]\n            windowOnes += bigArray[right++]\n            maxWindowOnes = Math.max(maxWindowOnes, windowOnes)\n        }\n        return ones - maxWindowOnes\n    }\n}\n```\n\n# Approach\n\nTo avoid unnecessary fiddling around with a looping array, I simply decided to make an array of size "x2" and search for a solution on this doubled array using a sliding window algorithm.\nThis will save time on the extra check for the transition of the right border of the window to the left border.\n\nTo copy the array I use the System.arraycopy() operation - this is the fastest that the virtual machine can offer for copying the contents of arrays.\n\nAfter the array is created, we no longer need to bother with possible indexes going beyond the "nums.size", so we can focus entirely on finding the result.\n\nWe use the sliding window algorithm. To find a solution, first count all the "1" numbers inside the array and write it to the variable "ones". Next, I define a window of size "ones" and start moving it inside the array "nums".\n\nThe best window will be one that collects the maximum possible number of "1" numbers from "nums".\n\nThe minimum number of swap operations will be the difference between the total number of 1s and the maximum number of 1s that got into the window.\n\nExample 1:\n![image.png](https://assets.leetcode.com/users/images/da096645-881e-4450-9074-ba2b4fe3808a_1722585127.1677387.png)\n\nExample 2:\n![image.png](https://assets.leetcode.com/users/images/67c79887-db99-4a31-947d-d5ceed5ae384_1722586212.375604.png)\n\nOne more attempt (one pointer sliding window):\n\n![image.png](https://assets.leetcode.com/users/images/4d0183b7-9ef9-4127-9285-2679d78372fc_1722598671.8778625.png)\n\n```\nclass Solution {\n    fun minSwaps(nums: IntArray): Int {\n        var ones = 0\n        for (n in nums) {\n            ones += n\n        }\n\n        var windowOnes = 0\n        for (i in 0 until ones) {\n            windowOnes += nums[i]\n        }\n        if (windowOnes == ones) return 0\n\n        var maxWindowOnes = windowOnes\n\n        val bigArray = IntArray(nums.size shl 1)\n        System.arraycopy(nums, 0, bigArray, 0, nums.size)\n        System.arraycopy(nums, 0, bigArray, nums.size, nums.size)\n\n        for (i in 0 until nums.size) {\n            windowOnes -= bigArray[i]\n            if (bigArray[i + ones] == 1) {\n                maxWindowOnes = Math.max(maxWindowOnes, ++windowOnes)\n            }\n        }\n        return ones - maxWindowOnes\n    }\n}\n```\n
['Sliding Window', 'Kotlin']