Data Structures & AlgorithmsHardCodeforces Div2 B · digit patterns

Problem: How many integers in [1, 100] have non-decreasing decimal digits (for example 1245 or 3399, but 2 digits only here)? Run the code.

Problem: How many integers in [1, 100] have non-decreasing decimal digits (for example 1245 or 3399, but 2 digits only here)? Run the code.
def nd(x):
    s = str(x)
    return all(s[i] <= s[i + 1] for i in range(len(s) - 1))
print(sum(1 for x in range(1, 101) if nd(x)))
What is printed?
  1. A. 45
  2. B. 55
  3. C. 54
  4. D. 63

Correct answer: C. 54

All 9 one-digit numbers qualify, 45 two-digit numbers have non-decreasing digits, and 100 fails; the total is 54.

More Coding Interview Problems questions

Exams that ask this topic