Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 C

Problem: In the binary string "1101001110100110", what is the length of the longest contiguous substring containing equal numbers of 0s and 1s?

  1. A. 6
  2. B. 35
  3. C. 14
  4. D. 16

Correct answer: C. 14

Map 1 → +1 and 0 → −1 and keep the FIRST index of each prefix balance (seeded with 0 at −1); the largest gap is 14. Overwriting with the latest index gives 6, counting balanced substrings gives 35, and 16 assumes the whole string is balanced although it has 9 ones and 7 zeros.

More Coding Interview Problems questions

Exams that ask this topic