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?
- A. 32
- B. 25
- C. 29
- 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.
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
- Each turn you pick a number x from [3, 4, 2, 4, 4, 6], earn x points, and then delete EVERY copy of x−1 and x+1. You may keep doing this…
- How many contiguous subarrays of 2 5 1 3 5 2 4 have a sum EXACTLY equal to 5, the maximum value of the whole array?
- How many contiguous subarrays of length exactly 3 in 1 2 3 4 4 1 5 7 have bitwise XOR equal to 0?
- How many contiguous subarrays of 3 1 4 1 5 9 2 have (sum − length) divisible by 4?
- How many contiguous subarrays of 1 2 3 4 5 6 have a sum of SQUARES divisible by 5?
- For each contiguous subarray of 4 2 7 1 5 3 6 2, locate the FIRST position (in the original array) where its minimum value occurs. How…