Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Greedy/Greedy

Pattern · CP 1600

Constructive Algorithms

Prove when an object (array, permutation, grid, graph) exists, and build it directly. Usually: solve tiny cases by hand, spot the pattern, and handle a few parity or size cases.

Learn firstGreedyMath / Number Theory
0%
mastery
0/9 solved
Practise nextInverse Inversions
IdeaPracticePitfalls

Recognise it

The signal

'output any array/permutation/grid that satisfies ... or -1'

  1. 1Brute force

    Backtracking over all objects (n! or 2n).

  2. 2The observation

    The search space. The trick is a structural insight.

  3. 3Why it works

    The construction keeps a property that implies validity (e.g. each block adds exactly one to the LIS).

Reach for it when

  • +existence depends on n mod 2/3/4
  • +construct with exactly k inversions / a given LIS length / given degrees

Not when

  • ×the answer must be optimal among many candidates (DP / greedy proofs)
  • ×n is tiny (backtracking is fine)

Practice

Next

Inverse Inversions

0/9 solved
  • PermutationsCSESEasy
  • Inverse InversionsCSESMedium
  • Monotone SubsequencesCSESMedium
  • Chess TournamentCSESMedium
  • Gray CodeCSESMedium
  • Special PermutationCodeforces1600
  • Beautiful Arrangement IIMedium
  • Gray CodeMedium
  • Third PermutationCSESHard

Pitfalls

Misconceptions

  • !skipping the brute force on small n that shows the pattern and the -1 cases
Go deeperresources

Resources

  • Greedy Algorithms with SortingUSACO Guide · course
  • Greedy Algorithms (chapter)Jeff Erickson · book
  • Exchange arguments (sorting with dp)Errichto · blog
  • Algorithms playlistAbdul Bari · video
  • Competitive Programmer's HandbookAntti Laaksonen · book
TodayReviewPatternsProblems