diff --git a/C++/sort-array-by-parity.cpp b/C++/sort-array-by-parity.cpp new file mode 100644 index 000000000..cfa3e2641 --- /dev/null +++ b/C++/sort-array-by-parity.cpp @@ -0,0 +1,14 @@ +// Time: O(n) +// Space: O(1) + +class Solution { +public: + vector sortArrayByParity(vector& A) { + for (int i = 0, j = 0; j < A.size(); ++j) { + if (A[j] % 2 == 0) { + swap(A[i++], A[j]); + } + } + return A; + } +};