JAVA | Two solutions | Extra space and In-place
Please Upvote !!! (\u25E0\u203F\u25E0)\n##### 1. Using extra-space (stack):\n\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        Sta
### ***Please Upvote !!!*** **(\u25E0\u203F\u25E0)**\n##### 1. Using extra-space (stack):\n```\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        Stack<Integer> evens = new Stack<>();\n        Stack<Integer> odds = new Stack<>();\n\n        for (int n : nums) {\n            if (n % 2 == 0) evens.add(n);\n            else odds.add(n);\n        }\n\n        for (int i = 0; i < nums.length; i++) {\n            if (i % 2 == 0) nums[i] = evens.pop();\n            else nums[i] = odds.pop();\n        }\n\n        return nums;\n    }\n}\n\n// TC: O(n), SC: O(n)\n```\n##### 2. In-place: \n```\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        int i = 0, j = 1;\n        int n = nums.length;\n\n        while (i < n && j < n) {\n            while (i < n && nums[i] % 2 == 0) i += 2;\n            while (j < n && nums[j] % 2 == 1) j += 2;\n\n            if (i < n && j < n) {\n                int temp = nums[i];\n                nums[i] = nums[j];\n                nums[j] = temp;\n            }\n        }\n\n        return nums;\n    }\n}\n\n// TC: O(N), SC: O(1)\n```
['Two Pointers', 'Stack', 'Java']