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?
- A. 4
- B. 5
- C. 7
- 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.
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
- What is the maximum product of a non-empty contiguous subarray of [3, 4, 3, 3, −2, −2, −1, 3]? (Track both the maximum and the minimum…
- Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [3, 37, 10, 52, 30, 23…
- Records are sorted with a composite key. What is printed? python recs = [('anu', 80), ('zoya', 70), ('dev', 70), ('ravi', 70), ('meera'…
- Two sorted arrays of sizes m and n (m ≤ n) are given. What is the best possible time to find the median of their union?
- A circular queue in an array of size n uses front and rear indices and keeps one slot always empty. What is the maximum number of elements…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "bbaabbbabaaa", "aa" over = sum(1 for i in…