↓Skip to main content
  1. LeetCode/

LeetCode 167: Two Sum II - Input Array Is Sorted

·2 mins· ·
LeetCode Medium Array Two-Pointers Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-27

  • Submissions: Runtime Error ×2 → Wrong Answer → Runtime Error ×2 → Wrong Answer ×5 → Accepted → Wrong Answer ×3 → Accepted ×2 → Wrong Answer → Accepted

Approach
#

The array is sorted (Two Pointers Template 1): the left pointer at the smallest value and the right at the largest. If the sum is too large move the right pointer left (a smaller value), if too small move the left pointer right. Each step rules out one value, O(n). Indices are 1-based.

My Attempts
#

This took 18 submissions in 2022:

  • At first both pointers started on the left and moved together when the sum was too large, never finding the answer or running out of range, e.g. [2, 3, 5], target = 8.
  • After starting from both ends, one version wrote l -= 1 when the sum was too small, the wrong direction: [1, 4], target = 5 was not found. A 2026 rewrite forgot that indices are 1-based and returned [0, 1].

Solution
#

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        l, r = 0, len(numbers) - 1
        while l < r:
            s = numbers[l] + numbers[r]
            if s == target:
                return [l + 1, r + 1]
            if s > target:
                r -= 1
            else:
                l += 1

Complexity
#

Time O(n), space O(1). Binary searching the complement of each value is O(n log n).

Mistakes to Avoid
#

  • When the sum is too small, the left pointer moves right (l += 1), not left.
  • Add 1 to the returned indices.

Interview Explanation
#

With a sorted array I start at both ends, moving the right pointer left when the sum is too large and the left pointer right when too small, returning the 1-based indices on a match. Time O(n), space O(1).

Related

LeetCode 75: Sort Colors
·2 mins
LeetCode Medium Array Two-Pointers Sorting Quicksort
LeetCode 75 note: the Dutch flag, with 0s left of low and 2s right of high; after swapping a 2 to high, do not advance, since the swapped-in value is unchecked
LeetCode 80: Remove Duplicates from Sorted Array II
·1 min
LeetCode Medium Array Two-Pointers
LeetCode 80 note: keep each value at most twice by comparing with nums[k - 2], two slots back from the write position
LeetCode 81: Search in Rotated Sorted Array II
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 81 note: with duplicates, nums[l] == nums[mid] hides which half is sorted, so just move l right by one; worst case O(n)
LeetCode 74: Search a 2D Matrix
·2 mins
LeetCode Medium Array Binary-Search Matrix
LeetCode 74 note: each row continues the previous one in sorted order, so binary search it as a 1-D array of length m × n, mapping mid to (mid // n, mid % n)
LeetCode 31: Next Permutation
·2 mins
LeetCode Medium Array Two-Pointers
LeetCode 31 note: from the right find the first dip i, swap it with the smallest larger value to its right, then reverse everything after i, with comparisons that include equality
LeetCode 33: Search in Rotated Sorted Array
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 33 note: after rotation one side of mid is always sorted; check whether the target lies in it and search there, otherwise search the other side