Data Structures & AlgorithmsVery hard
The 3-sum two-pointer method skips a value when it equals the previous value. Why?
- A. To make the algorithm run in O(1) space.
- B. To prevent counting the same triple of values more than once when the array contains repeated values.
- C. Because repeated values always have sum zero.
- D. Because the answer must be the number of distinct values.
Correct answer: B. To prevent counting the same triple of values more than once when the array contains repeated values.
After sorting, equal values produce identical triples, so skipping repeated first elements and pointer positions yields each distinct triple exactly once.
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 lexicographically smallest array of length n with positive integers and total sum S (S ≥ n)?
- In the 'minimum swaps to sort a permutation' problem, why is the answer n minus the number of cycles?
- Which statement about a monotonic deque for sliding-window minimum is correct?
- Why is prefix-sum counting of zero-sum subarrays unaffected by the choice of starting index for the prefix array?
- For the sum-at-most-K problem on a prefix-sum array with negative values, why does a balanced search structure appear in the solution?
- The shunting-yard algorithm converts the infix expression "a+bc^d^e-f^(gh)" to postfix, with ^ highest precedence and right-associative…