Java Solution beats 99% ;)
Intuition\nImagine you have a group of children standing in a line, and you want to arrange them such that every child in an even position (0, 2, 4, ...) is wea
# Intuition\nImagine you have a group of children standing in a line, and you want to arrange them such that every child in an even position (0, 2, 4, ...) is wearing a red shirt and every child in an odd position (1, 3, 5, ...) is wearing a blue shirt. However, right now the children are standing in a random order, and you need to rearrange them according to the shirt color rule.\n\nHere\u2019s how you can accomplish this task in a structured way:\n\n # Initialization:\n\n### You start with two pointers: \np1 at position 0 (the first position) and p2 at position 1 (the second position). These pointers will help you find and fix misplaced children.\n### Scanning and Fixing Misplacements:\n\nYou begin to move p1 through the line, but only at even positions (0, 2, 4, ...), checking if the child in that position is wearing a red shirt.\n\nSimilarly, you move p2 through the line, but only at odd positions (1, 3, 5, ...), checking if the child in that position is wearing a blue shirt.\n\nIf p1 finds a child at an even position who is not wearing a red shirt, it stops.\n\nIf p2 finds a child at an odd position who is not wearing a blue shirt, it stops.\n\n### Swapping Misplaced Children:\n\nOnce both p1 and p2 have stopped (both found misplacements), you swap the children at these positions. This exchange corrects the order for both the even and odd positions.\nContinuing the Process:\n\nYou continue this process until one of the pointers goes beyond the end of the line, meaning you\'ve checked all the positions.\nBy following this method, you ensure that every child at an even position wears a red shirt, and every child at an odd position wears a blue shirt. \nLet\'s look at the code implementing this:\n\n# Code\n```\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        int p1=0;\n        int p2=1;\n        int n=nums.length;\n        while(p1<n && p2<n){\n            while(p1<n && nums[p1]%2==0)p1+=2;\n            while(p2<n && nums[p2]%2!=0)p2+=2;\n            if(p1>=n || p2>=n)break;\n            int x=nums[p1];\n            nums[p1]=nums[p2];\n            nums[p2]=x;\n        }\n        return nums;\n    }\n}\n```
['Java']