class Solution {
public:
  void sortColors(int A[], int n) {
    int zeroPos = -1, twoPos = n, idx = 0;
    while (idx < twoPos) {
      if (A[idx] == 1) ++idx;
      else if (A[idx] == 0) swap(A[idx++], A[++zeroPos]);
      else swap(A[idx], A[--twoPos]);
    }
  }
};