Easy to Understand Java Code || Beats 100%
Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        int i =
# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        int i = 0, j = 1, n = nums.length;\n        while (i < n && j < n) {\n            while (i < n && nums[i] % 2 == 0) {\n                i += 2;\n            }\n            while (j < n && nums[j] % 2 == 1) {\n                j += 2;\n            }\n            if (i < n && j < n) {\n                swap(nums, i, j);\n            }\n        }\n        return nums;\n    }\n    private void swap(int[] nums, int i, int j) {\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n}\n```
['Java']