Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Dutch National Flag Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Dutch National Flag | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Sort an array A where each of the elements belongs to the set: {0, 1, 2}.

Naive Approach

Just sort the array using any in-place sorting algorithm.

Analysis

  • Time Complexity: O(n * logn)
  • Space Complexity: O(1)

Implementation

C++
void sortTheArray (vector<int> &A) {
   sort(A.begin(), A.end());
}
Java
class Solution {
   void sortTheArray (int[] A) {
       Arrays.sort(A);
   }
}

Optimal Approach

The strategy is to exploit the fact that the array consists only 3 types of elements, {0, 1, 2}. Also, doing it in constant space hints at some swapping operation.

We will divide the array into 4 regions using 3 pointers, say low, mid, and high. The region [0, low - 1] will represent all zeroes, [low, mid - 1] will represent all ones, [mid, high - 1] will represent a yet unknown region, and [high, N] will represent the region with all twos.

The target is to shrink the size of the unknown region to zero when the algorithm converges. If the current element is 0, swap it into the low range, if the current element is 1, just move the mid pointer ahead, else swap the element into the high range. In this way, in every move, we reduce the size of the unknown region, and at the end, we get the sorted array.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
void sortTheArray (vector<int> &A) {
   int low = 0, mid = 0, high = (int)A.size() - 1;
   while(mid <= high) {
       if((A[mid]) == 0) {
           swap(A[low], A[mid]);
           low++;
           mid++;
       } else if(A[mid] == 1) {
           mid++;
       } else {
           swap(A[mid], A[high]);
           high--;
       }
   }
}
Java
class Solution {
   void swap(int[] A, int first, int second) {
       int temp = A[first];
       A[first] = A[second];
       A[second] = temp;
   }
   void sortTheArray (int[] A) {
       int low = 0, mid = 0, high = A.length - 1;
       while(mid <= high) {
           if((A[mid]) == 0) {
               swap(A, low, mid);
               low++;
               mid++;
           } else if(A[mid] == 1) {
               mid++;
           } else {
               swap(A, mid, high);
               high--;
           }
       }
   }
}
Related Content
k-diff pairs
K-Subarray Sum
k-Substring Vowels
Kth element of two sorted lists
Maximum K-Subarray Sum
Maximum k-Substring Vowels
Merge Two Sorted Arrays
Remove occurences
Sorted Arrays Intersection
Three Sum
Trapping Rain Water
Two Sum Sorted
Unique Elements in Sorted Array
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet