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?
- A. 14
- B. 4
- C. 2
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many contiguous subarrays of 4 2 3 6 5 7 1 have length equal to (maximum − minimum + 1)?
- What is the length of the longest contiguous subarray of 3 -2 0 4 -1 0 2 -5 in which the number of positive elements equals the number of…
- A window with at most K replacements (or at most K zeros) is found with two pointers. Why is it safe for the left pointer to only move…
- In 'product of array except self', why does a single zero need special handling?
- Why does counting subarrays with sum exactly k, when elements can be NEGATIVE, use a hash map of prefix sums and not a sliding window?
- When counting pairs with a given sum in a sorted array using two pointers, why is sorting essential?