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.

  1. A. 1 1 1 1
  2. B. 1 1 2 4
  3. C. 4 4 3 1
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic