Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created February 2, 2026 22:17
Show Gist options
  • Select an option

  • Save Ephraimiyanda/bcb059d9cf60583cf9b9ff2b13124c9b to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/bcb059d9cf60583cf9b9ff2b13124c9b to your computer and use it in GitHub Desktop.
Two Sum II - Input Array Is Sorted

Question

Approach

  1. An indexes array is created to store the index integers.
  2. A double loop is created to check which numbers when added are equal to the target.
  3. if the sum of the numbersisequal to the target, "1" is added to their indexes which is then pushed to the indexes array.

Complexity

  • Time complexity: O(N^2)

  • Space complexity: O(1)

Code

function twoSum(numbers: number[], target: number): number[] {
    let indexes: number[] = []
    for (let i = 0; i < numbers.length; i++) {
        let j = i + 1
        while (j < numbers.length) {
            if (numbers[j] + numbers[i] === target) {
                indexes.push( i + 1,j + 1)
            }
            j++
        }
    }
    return indexes
};
scrnli_1KqvtN8Rcq51SJ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment