Data Structures & AlgorithmsVery hard
Among all arrays of length 4 with values in {1, 2, 3, 4}, the absolute differences between ADJACENT elements (three of them) must all be DIFFERENT. Which such array is lexicographically smallest? Give it as four space-separated numbers.
- A. 1 1 1 1
- B. 1 1 2 4
- C. 4 4 3 1
- D. 1 2 3 4
Correct answer: B. 1 1 2 4
Brute force over all 256 arrays keeps those whose three adjacent absolute differences are pairwise distinct, and the lexicographic minimum is the first in dictionary order. Allowing repeated differences, taking the maximum, or sorting the answer gives different arrays.
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
- Among all permutations of 1, 2, 3, 4, 5 with NO fixed point (no position i holds the value i), which permutation is lexicographically…
- Among binary strings of length 6 that contain exactly three '1's and no two '1's adjacent, which one is lexicographically smallest?
- How many contiguous windows of length exactly 4 in 2 5 1 4 7 3 6 2 9 have DISTINCT elements AND a sum divisible by 3?
- How many contiguous subarrays of 0 1 2 0 1 2 have a sum between 3 and 5 inclusive?
- How many NON-EMPTY subsequences of 3 1 4 1 5 9 (position subsets read in order) are STRICTLY increasing?
- How many NON-EMPTY subsequences of 2 3 4 7 8 5 (position subsets read in order) alternate in parity, so that each chosen value has the…