Skip to content
We areTOP 1%
DSA · Competitive Programmer · Pattern 3 of 9

Constructive Algorithms

15 problems.
Your progress0%
1st Visit
0/15
2nd Visit
0/15
Revise
0/15

About this pattern

Constructive problems ask you to build any valid object, such as a permutation, an array, a string or a sequence of operations, rather than compute a single number. There is rarely a standard algorithm; progress comes from solving small cases by hand, spotting an invariant or a pattern, and proving that a simple scheme always works. Useful habits are to derive necessary conditions first, since they often expose the impossible cases, to aim for a canonical intermediate state such as an all-equal or sorted array, and to replay self-inverse operations backwards. Signals: "print any", a bound like "at most 2n operations", or "-1 if impossible". Always test the construction on small inputs.

// Stress-test a construction against brute force for small n
bool valid(const vector<int>& p);       // checks the required property
vector<int> construct(int n);           // your scheme; empty means "impossible"

bool existsBrute(int n) {
    vector<int> p(n);
    iota(p.begin(), p.end(), 1);
    do { if (valid(p)) return true; } while (next_permutation(p.begin(), p.end()));
    return false;
}
void stress() {
    for (int n = 1; n <= 8; n++) {
        vector<int> p = construct(n);
        bool ok = p.empty() ? !existsBrute(n) : valid(p);
        if (!ok) cerr << "construction fails for n = " << n << '\n';
    }
}

Problems

Constructive Algorithms questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Beautiful Arrangement IILeetCodeMedium Solve Beautiful Arrangement II on LeetCode (opens in a new tab)
2Special PermutationCodeforcesMedium 1600Solve Special Permutation on Codeforces (opens in a new tab)
3Ehab and Prefix MEXsCodeforcesMedium 1600Solve Ehab and Prefix MEXs on Codeforces (opens in a new tab)
4Prefix Flip (Hard Version)CodeforcesMedium 1700Solve Prefix Flip (Hard Version) on Codeforces (opens in a new tab)
5Construct Target Array With Multiple SumsLeetCodeHard Solve Construct Target Array With Multiple Sums on LeetCode (opens in a new tab)
6Find the Closest PalindromeLeetCodeHard Solve Find the Closest Palindrome on LeetCode (opens in a new tab)
7A/B MatrixCodeforcesHard 1900Solve A/B Matrix on Codeforces (opens in a new tab)
8Phoenix and ScienceCodeforcesHard 1900Solve Phoenix and Science on Codeforces (opens in a new tab)
9Replace by MEXCodeforcesHard 1900Solve Replace by MEX on Codeforces (opens in a new tab)
10Engineer ArtemCodeforcesHard 2000Solve Engineer Artem on Codeforces (opens in a new tab)
11Orac and MediansCodeforcesHard 2000Solve Orac and Medians on Codeforces (opens in a new tab)
12Make Them EqualCodeforcesHard 2000Solve Make Them Equal on Codeforces (opens in a new tab)
13Transform to ChessboardLeetCodeHard Solve Transform to Chessboard on LeetCode (opens in a new tab)
14Errich-Tac-Toe (Easy Version)CodeforcesHard 2100Solve Errich-Tac-Toe (Easy Version) on Codeforces (opens in a new tab)
15Multiples and Power DifferencesCodeforcesHard 2200Solve Multiples and Power Differences on Codeforces (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account