Data Structures & AlgorithmsHard

Choose three DIFFERENT positions from -4 -1 1 2 5 7 and add their values. Among all such three-element sums that are closest to 3 (in absolute distance), what is the LARGEST one?

  1. A. 14
  2. B. 4
  3. C. 2
  4. D. 3

Correct answer: D. 3

Sort the array, fix the smallest index and move two pointers toward the target while tracking the best distance. Ties must be broken by the larger sum as the statement says, which a plain 'first closest found' scan does not guarantee.

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

Exams that ask this topic