Sort Array by Parity Problem & Solution

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.

See the sort array by parity problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
  vector<int> sortArrayByParity(vector<int>& nums) {
    int low = 0;
    int high = nums.size() - 1;
    while (low < high) {
      if (nums[low] % 2 != 0) {
        swap(nums[low], nums[high--]);
      } else {

    return nums;

Start Here

Many paths, there are. Follow yours, you must.