Sort Colors (Dutch National Flag)

Two Pointers & Sliding Window DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Sorting an array of 0s, 1s, and 2s in-place without inbuilt sort, using O(N) time and O(1) space (Dutch National Flag problem), Arrays, Sorting, Time Complexity, Space Complexity, Two Pointers, Frequency Counting, in-place algorithms, counting sort, dutch national flag algorithm, space complexity, sorting algorithms, time complexity analysis, Counting Sort, Partitioning Strategies, In-Place Algorithms.

Given an array nums consisting of only 0s, 1s, and 2s, sort the array in place without using any built in sort functions. The solution should aim for a single pass (O(N) time complexity) and constant space (O(1) space complexity). Input Specification: The input consists of a single line containing space separated integers representing the elements of the array nums. Output Specification: Print the sorted array nums with elements separated by spaces. Constraints: The array nums will only contain integers 0, 1, or 2. The sort must be in place. The solution should ideally run in O(N) time and O(1) space. Sample Test Cases: Sample Input 1: Sample Output 1: Sample Input 2: Sample Output 2: Sample Input 3: Sample Output 3: