[Python] O(1) space solution, explained
O(n) space complexity soluiton is straightforward. It is more interseting to investigate O(1) solution. The idea is to use two pointers approach, where we start
`O(n)` space complexity soluiton is straightforward. It is more interseting to investigate `O(1)` solution. The idea is to use two pointers approach, where we start with index `0` for even numbers and with index `1` for odd numbers. We traverse our numbers, where we can have the following options:\n\n1. if `nums[i] % 2 == 0`, then number is already on place, so we look at the next place for `i`.\n2. if `nums[j] % 2 == 1`, then number is already on place, so we look ate the next place for `j`.\n3. In the opposite case we need to sweich elements.\n\n\n#### Complexity\nTime complexity is `O(n)`, space complexity is `O(1)`.\n\n#### Code\n```python\nclass Solution:\n    def sortArrayByParityII(self, nums):\n        i, j, n = 0, 1, len(nums)\n        while j < n and i < n:\n            if nums[i] % 2 == 0:\n                i += 2\n            elif nums[j] % 2 == 1:\n                j += 2\n            else:\n                nums[i], nums[j] = nums[j], nums[i]\n        return nums\n```
['Two Pointers']