Data Structures & AlgorithmsModerate
How many arrays of length 4 with values from {1, 2, ..., 3} have no two adjacent elements equal?
- A. 54
- B. 48
- C. 24
- D. 78
Correct answer: C. 24
Pick the first value in m ways and each later value in m−1 ways, giving m·(m−1)^(n−1). Multiplying the (m−1) choice onto the wrong position, or subtracting only the constant sequences, gives the other totals.
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
- Each step adds 1 to one element of [2, 2, 2, 5, 5, 1]. What is the minimum total number of steps so that every element becomes distinct?…
- A sorted array [1, 5, 10] is given. You may insert new positive integers into it. What is the minimum number of insertions so that every…
- What is the smallest positive integer that CANNOT be written as the sum of some subset (each element used at most once) of the sorted…
- For the array [1, -2, 3, -1, 4, -6, 2], what is the maximum possible sum of a contiguous non-empty subarray after deleting AT MOST one of…
- In the binary array [1, 1, 0, 1, 1, 1, 0, 1], you must delete exactly one element. What is the length of the longest run of consecutive 1s…
- How many index triples i < j < k in the sorted array [-2, 0, 1, 3, 5, 6] have A[i] + A[j] + A[k] strictly less than 4?