Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE

Problem: A 5×5 grid has 1s and 0s: 11000 / 11011 / 00100 / 01100 / 10001 (rows top to bottom). You may flip exactly one 0 into a 1. What is the largest possible 4-directionally connected component of 1s afterwards?

  1. A. 4
  2. B. 10
  3. C. 12
  4. D. 11

Correct answer: B. 10

Label components first (sizes 4, 3, 2, 1, 1), then for each 0 add 1 plus the DISTINCT neighbouring components; the best flip gives 10. The largest component before any flip is 4, allowing diagonal connections gives 12, and adding neighbour sizes without de-duplicating a component touched from two sides gives 11.

More Coding Interview Problems questions

Exams that ask this topic