Data Structures & AlgorithmsVery hardLeetCode-style · GATE CS
A fence has 5 posts and each post is painted one of k = 3 colours. No three consecutive posts may share the same colour. How many colourings are possible?
- A. 186
- B. 180
- C. 243
- D. 66
Correct answer: B. 180
Track two states for each length: the number of valid colourings ending in a run of length 1 (diff) and in a run of length 2 (same). A new post either starts a new colour (k − 1 options from the previous colour) or extends a run of length 1; runs longer than 2 are forbidden.
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
- Target string "abcbc" must be built by concatenating words from the list ['ab', 'bc', 'abc', 'a', 'b', 'c', 'cb']; each word can be…
- A truck carries at most 5 boxes. Box types are given as (number of boxes of this type, units per box): [(1, 3), (2, 2), (3, 1), (2, 4)]…
- How many contiguous subarrays of 2 3 4 1 6 9 have a product divisible by 6?
- How many contiguous subarrays of 5 2 7 1 8 3 6 4 have an ODD length and a sum divisible by 7?
- What is the sum, over all non-empty contiguous subarrays of 5 3 6 1 4, of their XOR values, taken modulo 1000?
- How many arrays of length 3 with entries in {0, 1, 2, 3} have a sum divisible by 4?