`{"id":"91e5be15-6ce2-4650-a237-b629562278b7","name":"Sort Array By Parity","description":"1. Given an array nums of non-negative integers.\r\n2. Arrange elements of array in specific order, all even elements followed by odd elements. [even -> odd]\r\n3. Preserve the order of Even elements without using extra space.\r\n4. Hence question is order specific so you have to match your output in order of given output.\r\n","inputFormat":"7\r\n9 3 8 7 6 2 3\r\n","outputFormat":"8 6 2 7 3 9 3 \r\nExplanation : {even -> odd}, 8 is before 6 in given input as well as in output, same for 6,2. ","constraints":"1. 1 &lt;= nums.length &lt;= 5000\r\n2. 0 &lt;= nums[i] &lt;= 5000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <string>\n#include <cmath>\nusing namespace std;\n \nvoid sortArrayByParity(vector<int> &nums) {\n // write your code here\n}\n\nint main() {\n int n = 0;\n cin>>n;\n vector<int> arr(n);\n for(int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n sortArrayByParity(arr);\n for(int i = 0; i < n; i++) {\n cout<<arr[i]<<\" \";\n }\n return 0;\n}\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~\r\n \r\n public static void sortArrayByParity(int[] nums) {\r\n // write your code here\r\n }\r\n\r\n //// ~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n sortArrayByParity(arr);\r\n\r\n for(int i = 0; i < n; i++) {\r\n System.out.print(arr[i] + \" \");\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n9 3 8 7 6 2 3","sampleOutput":"8 6 2 7 3 9 3 \r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"87f5db85-73c8-4ca4-a1af-a3efad34b963","name":"Sort Array By Parity","slug":"sort-array-by-parity","type":1}],"next":{"id":"3380da56-4e7d-4887-ade9-aeb237ef87f3","name":"Sort Array by Parity","type":3,"slug":"sort-array-by-parity"},"prev":{"id":"b758bbf5-05e7-4edf-b1e0-de0579868146","name":"Multiply Strings MCQ","type":0,"slug":"multiply-strings-mcq"}}}`

# Sort Array By Parity

1. Given an array nums of non-negative integers. 2. Arrange elements of array in specific order, all even elements followed by odd elements. [even -> odd] 3. Preserve the order of Even elements without using extra space. 4. Hence question is order specific so you have to match your output in order of given output.

`{"id":"91e5be15-6ce2-4650-a237-b629562278b7","name":"Sort Array By Parity","description":"1. Given an array nums of non-negative integers.\r\n2. Arrange elements of array in specific order, all even elements followed by odd elements. [even -> odd]\r\n3. Preserve the order of Even elements without using extra space.\r\n4. Hence question is order specific so you have to match your output in order of given output.\r\n","inputFormat":"7\r\n9 3 8 7 6 2 3\r\n","outputFormat":"8 6 2 7 3 9 3 \r\nExplanation : {even -> odd}, 8 is before 6 in given input as well as in output, same for 6,2. ","constraints":"1. 1 &lt;= nums.length &lt;= 5000\r\n2. 0 &lt;= nums[i] &lt;= 5000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <string>\n#include <cmath>\nusing namespace std;\n \nvoid sortArrayByParity(vector<int> &nums) {\n // write your code here\n}\n\nint main() {\n int n = 0;\n cin>>n;\n vector<int> arr(n);\n for(int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n sortArrayByParity(arr);\n for(int i = 0; i < n; i++) {\n cout<<arr[i]<<\" \";\n }\n return 0;\n}\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~\r\n \r\n public static void sortArrayByParity(int[] nums) {\r\n // write your code here\r\n }\r\n\r\n //// ~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n sortArrayByParity(arr);\r\n\r\n for(int i = 0; i < n; i++) {\r\n System.out.print(arr[i] + \" \");\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n9 3 8 7 6 2 3","sampleOutput":"8 6 2 7 3 9 3 \r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"87f5db85-73c8-4ca4-a1af-a3efad34b963","name":"Sort Array By Parity","slug":"sort-array-by-parity","type":1}],"next":{"id":"3380da56-4e7d-4887-ade9-aeb237ef87f3","name":"Sort Array by Parity","type":3,"slug":"sort-array-by-parity"},"prev":{"id":"b758bbf5-05e7-4edf-b1e0-de0579868146","name":"Multiply Strings MCQ","type":0,"slug":"multiply-strings-mcq"}}}`

Editor

# Sort Array By Parity

easy

1. Given an array nums of non-negative integers. 2. Arrange elements of array in specific order, all even elements followed by odd elements. [even -> odd] 3. Preserve the order of Even elements without using extra space. 4. Hence question is order specific so you have to match your output in order of given output.

## Constraints

1. 1 <= nums.length <= 5000 2. 0 <= nums[i] <= 5000

## Format

7 9 3 8 7 6 2 3

### Output

8 6 2 7 3 9 3 Explanation : {even -> odd}, 8 is before 6 in given input as well as in output, same for 6,2.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 9 3 8 7 6 2 3```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}8 6 2 7 3 9 3 ```

Discussions

Show Discussion

Related Resources