Data Structures & AlgorithmsVery hard

What is the lexicographically smallest array of length n with positive integers and total sum S (S ≥ n)?

  1. A. S, 0, …, 0 with zeros replaced by ones
  2. B. S/n repeated n times
  3. C. 1, 1, …, 1, S − (n − 1)
  4. D. 1, 2, …, n with the last element adjusted

Correct answer: C. 1, 1, …, 1, S − (n − 1)

Lexicographic minimality puts 1s first, and the last element absorbs the remainder. Spreading the sum evenly makes the first element larger, which is lexicographically worse.

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

Exams that ask this topic