Observe that 2k for fixed k is O(1), and 2n is O(n). Construct a second array, and copy elements from the source array to the target array, adding elements with key false at one end and key true at the other. you can scan the array once to find out where the boundary must be.