<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.util.Date;
import java.util.Random;

public class DutchFlagSolution {
    /**
     * Rearrange values in `f` so that all "Red"s come first, followed by all
     * "White"s, followed by all "Blue"s.
     * Requires that each element of f is either "Red", "White", or "Blue".
     */
    static void dutchFlag(String[] f) {
        // TODO

        //  0     i     j     k     f.length
        // +-----------------------+
        // |  R  |  W  |  ?  |  B  |
        // +-----------------------+

        int i = 0;  // First element of white region
        int j = 0;  // First element of unknkown region
        int k = f.length;  // First element of blue region
        while (j &lt; k) {
            if (f[j].equals("Red")) {
                // Swap with first white element, expand red region and shift
                // white region
                swap(f, i, j);
                i += 1;
                j += 1;
            } else if (f[j].equals("White")) {
                // Keep in same place, expand white region
                j += 1;
            } else {  // f[j] is "Blue"
                // Swap with last unknown element, expand blue region left
                swap(f, j, k-1);
                k -= 1;
            }
        }
    }

    static void dutchFlag2(String[] f) {
        //  0     i     j     k     f.length
        // +-----------------------+
        // |  R  |  W  |  B  |  ?  |
        // +-----------------------+

        int i = 0;  // First element of white region
        int j = 0;  // First element of blue region
        int k = 0;  // First element of unknown region
        while (k &lt; f.length) {
            if (f[k].equals("Red")) {
                // Swap with first element after red region, then, if that
                // element was white, swap with first blue element; expand red
                // region and shift white and blue regions
                swap(f, i, k);
                if (j &gt; i) {
                    swap(f, j, k);
                }
                i += 1;
                j += 1;
                k += 1;
            } else if (f[k].equals("White")) {
                // Swap with first blue element, expand white region and shift
                // blue region
                swap(f, j, k);
                j += 1;
                k += 1;
            } else {  // f[k] is "Blue"
                // Keep in same place, expand blue region
                k += 1;
            }
        }
    }

    /** Swap elements at indices `i` and `j` in array `a`. */
    static void swap(String[] a, int i, int j) {
        String tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    public static void main(String[] args) {
        // Array of color choices (facilitates random selection)
        String[] rwb = new String[]{"Red", "White", "Blue"};

        // Random number generator (reporting and optionally setting seed
        // allows results to be reproduced)
        long seed;
        if (args.length &gt; 0) {
            seed = Long.parseLong(args[0]);
        } else {
            seed = new Date().getTime();
            System.err.println("Seed: " + seed);
        }
        Random rng = new Random(seed);

        // Generate random array of colors
        String[] colors = new String[72];
        for (int i = 0; i &lt; colors.length; ++i) {
            // Deliberately create new objects to flag bugs in equality tests
            colors[i] = new String(rwb[rng.nextInt(rwb.length)]);
        }

        // Run "Dutch National Flag" algorithm
        dutchFlag(colors);

        // Print results
        for (String s : colors) {
            char c = ' ';
            switch(s) {
                case "Red":
                    c = 'R'; break;
                case "White":
                    c = 'W'; break;
                case "Blue":
                    c = 'B'; break;
            }
            System.out.print(c);
        }
        System.out.println();
    }
}
</pre></body></html>