🚀100% || Two Arrays Solution || Same Array Solution || Commented Code🚀
Problem Description\n\nGiven an array of integers, rearrange the elements in a way that all even integers appear at the beginning of the array, followed by all 
# Problem Description\n\nGiven an array of integers, **rearrange** the elements in a way that all **even** integers appear at the **beginning** of the array, **followed** by all **odd** integers.\nThere can be **multiple** valid outputs that satisfy this condition.\n\n- **Constraints:**\n    - `1 <= nums.length <= 5000`\n    - `0 <= nums[i] <= 5000`\n    - Return any **valid** output.\n\n---\n\n# Proposed Approaches\n## Two Arrays Solution\n1. **Create** two containers: `evenNumbers` and `oddNumbers` to store even and odd integers respectively.\n2. **Iterate** through the input array nums.\n    - If an element is **even**, add it to the `evenNumbers` container.\n    - If an element is **odd**, add it to the `oddNumbers` container.\n3. **Combine** the even and odd numbers, placing even numbers first.\n4. **Return** the combined array of even and odd numbers.\n\n## Complexity\n- **Time complexity:**$$O(N)$$\nSince we are iteration two times, one time to append even and odd elements and another time to merge the arrays then it is `2 * N` which is `O(N)`.\n- **Space complexity:**$$O(N)$$\nSince we are storing the the integers in two arrays and returning the combining of them so the space complexity is `O(N)`.\n\n---\n## Same Array Solution\n1. Initialize an `evenIndex` variable to **track** the position for the next even element.\n2. **Iterate** through the input array nums.\n    - If an element is **even**, **swap** it with the next **available** even-indexed position using the swap function.\n    - **Increment** the evenIndex to mark the next **available** even position.\n3. **Return** the sorted array with even elements appearing first.\n\n## Complexity\n- **Time complexity:**$$O(N)$$\nSince we are iteration one time to detect and swap even and odd elements so time complexity is `O(N)`.\n- **Space complexity:**$$O(1)$$\nSince we are storing one variable so the complexity is `O(1)`\n\n\n\n\n---\n\n\n\n# Code\n## Two Arrays Solution\n```C++ []\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        vector<int> evenNumbers, oddNumbers;  // Containers for even and odd numbers\n        \n        // Separate even and odd numbers\n        for (int i = 0; i < nums.size(); i++) {\n            if (nums[i] % 2 == 0)\n                evenNumbers.push_back(nums[i]);  // Even number found\n            else\n                oddNumbers.push_back(nums[i]);   // Odd number found\n        }\n        \n        // Combine even and odd numbers, placing even numbers first\n        for (int num : oddNumbers) {\n            evenNumbers.push_back(num);\n        }\n        \n        return evenNumbers;  // Return the sorted array\n    }\n};\n```\n```Java []\nclass Solution {\n    public int[] sortArrayByParity(int[] nums) {\n        List<Integer> evenNumbers = new ArrayList<>();\n        List<Integer> oddNumbers = new ArrayList<>();\n        \n        // Separate even and odd numbers\n        for (int i = 0; i < nums.length; i++) {\n            if (nums[i] % 2 == 0)\n                evenNumbers.add(nums[i]);  // Even number found\n            else\n                oddNumbers.add(nums[i]);   // Odd number found\n        }\n        \n        // Combine even and odd numbers, placing even numbers first\n        for (int num : oddNumbers) {\n            evenNumbers.add(num);\n        }\n        \n        // Convert List to int[]\n        int[] result = new int[evenNumbers.size()];\n        for (int i = 0; i < evenNumbers.size(); i++) {\n            result[i] = evenNumbers.get(i);\n        }\n        \n        return result;  // Return the sorted array\n    }\n}\n```\n```Python []\nclass Solution:\n    def sortArrayByParity(self, nums: List[int]) -> List[int]:\n        even_numbers = []\n        odd_numbers = []\n        \n        # Separate even and odd numbers\n        for num in nums:\n            if num % 2 == 0:\n                even_numbers.append(num)  # Even number found\n            else:\n                odd_numbers.append(num)   # Odd number found\n        \n        # Combine even and odd numbers, placing even numbers first\n        result = even_numbers + odd_numbers\n        \n        return result  # Return the sorted array\n```\n\n---\n\n## Same Array Solution\n```C++ []\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        // Index to track the position of the next even element\n        int evenIndex = 0;  \n        \n        for (int i = 0; i < nums.size(); i++) {\n            if (nums[i] % 2 == 0) {\n                // Swap the current element with the next available even-indexed position\n                swap(nums[i], nums[evenIndex++]);\n            }\n        }\n        \n         // Return the sorted array\n        return nums; \n    }\n};\n```\n```Java []\nclass Solution {\n    public int[] sortArrayByParity(int[] nums) {\n        // Index to track the position of the next even element\n        int evenIndex = 0;  \n        \n        for (int i = 0; i < nums.length; i++) {\n            if (nums[i] % 2 == 0) {\n                // Swap the current element with the next available even-indexed position\n                swap(nums, i, evenIndex++);\n            }\n        }\n        \n        // Return the sorted array\n        return nums;  \n    }\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```\n```Python []\nclass Solution:\n    def sortArrayByParity(self, nums: list[int]) -> list[int]:\n        # Index to track the position of the next even element\n        even_index = 0 \n\n        for i in range(len(nums)):\n            if nums[i] % 2 == 0:\n                # Swap the current element with the next available even-indexed position\n                nums[i], nums[even_index] = nums[even_index], nums[i]\n                even_index += 1\n\n        # Return the sorted array\n        return nums\n```\n\n\n![leet_sol.jpg](https://assets.leetcode.com/users/images/e6a53a0d-270d-45ab-a434-f2d2d7cceb80_1695859989.1166193.jpeg)\n
['Array', 'Python', 'C++', 'Java', 'Python3']