Given an array arr[] of size N consisting of 0, 1, 2, and 3 only, the task is to sort the given array in ascending order.
Example:
Input: arr[] = {0, 3, 1, 2, 0, 3, 1, 2}
Output: 0 0 1 1 2 2 3 3Input: arr[] = {0, 1, 3, 1, 0, 1, 3, 2, 1, 2, 0, 3, 0, 0, 1}
Output: 0 0 0 0 0 1 1 1 1 1 2 2 3 3 3
Approach: The given problem can be solved based on the approach discussed in this article. The idea is first to position all the 0s and 3s at the beginning and end of the array, followed by sorting the occurrences of integers 1 and 2.
Follow the steps below to solve the problem:
- Initialize three variables, say i, mid, and j. Set the values of i and mid as 0 and j as (N – 1).
- Iterate a loop until mid ≤ j and perform the following steps:
- If the value of arr[mid] is 0, then swap arr[i] and arr[mid] and increment the value of i and mid by 1.
- Otherwise, if the value of arr[mid] is 3, then swap arr[mid] and arr[j] and decrement j by 1.
- Otherwise, if the value of arr[i] is either 1 or 2, then increment the value of mid by 1.
- Now to sort the subarray over the range [i, j] by iterating until i ≤ j and perform the following operations:
- If the value of arr[i] is 2, then swap arr[i] and arr[j] and decrement the value of j by 1.
- Otherwise, increment the value of i by 1.
- After completing the above steps, print the array arr[] as the resultant sorted array.
Below is the implementation of the above approach:
C++
|
0 0 1 1 2 2 3 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment