Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many strings of length 5 over the alphabet {0, 1, 2} have a digit sum divisible by 3 (the sum of their digit values)?
- A. 84
- B. 81
- C. 82
- D. 27
Correct answer: B. 81
Count by DP over the residue of the running sum mod 3: each new digit shifts the residue by 0, 1 or 2, so the three residue counts evolve linearly. The total 3ⁿ divided by 3 is only an estimate, since residues need not be equally likely at every length.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A robot moves from the top-left to the bottom-right of a 4 × 5 grid of cells, each step going right, down, or diagonally down-right. How…
- A gold mine is the grid [[1, 3, 3], [2, 1, 4], [0, 6, 4]]. You start in any cell of the LEFT column and move one column to the right each…
- How many permutations of 0, 1, 2, 3, 4 are alternating, meaning p[0] < p[1] > p[2] < p[3] > p[4]?
- What is the smallest positive integer x such that x! (x factorial) ends with at least 1000 zeros?
- How many substrings of "abcabcb" contain at least one each of the letters a, b and c?
- What is the length of the shortest substring of "aabcbcdbca" that contains every distinct letter appearing in the whole string at least…