Given an array
We will use the integers
You must solve this problem without using the library's sort function
Output: [0,0,1,1,2,2]
Output: [0,1,2]
Follow up:
Could you come up with a one-pass algorithm using only constant extra space?
Contents
Solution 1 - Count 0's, 1's and 2's, then places them in order Solution 2 - Using two pointers, move all 0's to left and 2's to the right side (answer to the follow-up question)
In this approach, we will first count number of
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1)
In this approach, we will use two pointers to indicate left and right ends of the array and another pointer to indicate the current position of array that
will go through from left to right. Then move all
implementation details:
-
We will first initialize two variables
left = 0 to start from left end of the array andright = nums.length -1 to start from right end of array. Also initialize another variablecurrent =0 that we will use to traverse through array. -
Then we will start looping through input array until
current reachesright , that means when these two intersect all0's will be on left side and2's will be on right side.-
If the element at
current index is0 , then swap elements atcurrent index withleft (by doing so, we have made sure that 0 elements are moved to left side), now increment bothcurrent andleft indexes. -
If the element at
current index is1 , then simply incrementcurrent index as 1 is in the right position. -
If the element at
current index is2 , then swap elements atcurrent index withright (by doing so, we have made sure that 2 elements are moved to right side), now decrementright index.
The reason we are not updatingcurrent index is because, after swappingright indexed element withcurrent the element swappet tocurrent index may be 2 again.
-
If the element at
Let's look at an example, say if the given input array is
-
We will first create two pointers
left = 0 andright = 5 , also createcurrent = 0 to iterate through elements. -
In the first iteration,
current = 0 indexed element is 2, so we swap it withright = 5 index, so the array now becomes[ , and0 ,0,2,1,1,2 ]right will become4 . -
In the second iteration,
current = 0 indexed element is 0, so we swap it withleft = 0 index, and the array now becomes[ and0 ,0,2,1,1,2]current index becomes1 andleft also becomes1 . -
In the third iteration,
current = 1 indexed element is 0, so we swap it withleft = 1 index, and the array now becomes[0, and0 ,2,1,1,2]current index becomes2 andleft also becomes2 . -
In the fourth iteration,
current = 2 indexed element is 2, so we swap it withright = 4 , and the array now becomes[0,0, , and1 ,1,2 ,2]right will become3 . -
In the fifth iteration,
current = 2 indexed element is 1, so we will incrementcurrent to 3. -
In the sixth iteration,
current = 3 indexed element is 1, so we will incrementcurrent to 4. -
Now
current = 4 index is creater thanright = 3 , so all elements are in order.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1)
Above implementations source code can be found at