Data Structures & AlgorithmsHardGATE CS · TCS NQT

Ropes of lengths [4, 3, 2, 6] are joined two at a time, and joining costs the sum of their lengths. You may choose the order of joins. What is the minimum total cost to join all ropes into one?

  1. A. 31
  2. B. 34
  3. C. 29
  4. D. 15

Correct answer: C. 29

Always join the two shortest ropes next (the Huffman principle, with a min-heap). Joining in the given order, or joining onto one growing rope, keeps re-adding the big rope and costs more.

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

Exams that ask this topic