Data Structures & AlgorithmsHardTCS Digital · Infosys SP

Sort [−1, 3, 2, 2, 6, 1, −2] and, for each i, move two pointers to find the triplet sum closest to 5. What is that closest sum?

  1. A. 4
  2. B. 5
  3. C. 7
  4. D. 6

Correct answer: B. 5

Sorting gives [−2, −1, 1, 2, 2, 3, 6]. Fix i, then if the current sum is below the target move lo right, else move hi left, tracking the smallest |sum − 5|. The closest achievable sum is 5.

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

Exams that ask this topic