Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT

Choose two different elements of [-8, 1, 5, 9, 12, 20, 33] whose sum is as close as possible to the target 30. What is that sum?

  1. A. 32
  2. B. 25
  3. C. 29
  4. D. 38

Correct answer: C. 29

Sort and move two pointers toward each other: 9 + 20 = 29 is 1 below 30, and no pair hits 30 exactly. Checking only adjacent elements finds 12 + 20 = 32; 25 (5 + 20) and 38 (5 + 33) are farther from the target.

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

Exams that ask this topic