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?- A. 45
- B. 55
- C. 54
- 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
- Problem: What is the spiral (clockwise from the top-left) traversal of the matrix [[1,2,3],[4,5,6],[7,8,9]]? Print the list produced by…
- Problem: How many swaps does insertion sort perform on [3, 2, 1]? Count the shifts in the code. python a = [3, 2, 1] swaps = 0 for i in…
- Problem: What is the minimum number of intervals from [[1,4],[3,7],[6,10],[9,10]] needed to cover the segment [1, 10]? Run the greedy…
- Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] XOR a[j] = 1? Run the code. python a = [1, 2, 3, 4, 5] print(sum(1 for i in…
- Problem: How many subarrays of [3, 4, 7, 2, -3, 1, 4, 2] have sum exactly 7? Run the prefix-sum hash code. python from collections import…
- Problem: What is the length of the shortest substring of 'aabcbcdbca' that contains all distinct characters of the string? Run the…