← Back

Two Sum II — Input Array Is Sorted

The canonical converging scan: one pass, no extra memory.

Medium LeetCode ↗ 1 / 1
func twoSum(numbers []int, target int) []int {

}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        case sum == target:
            return []int{left + 1, right + 1}
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        case sum == target:
            return []int{left + 1, right + 1}

        // Sum too small: the left value is the weakest link. Move left up to a
        // bigger number, which is the only way to increase the sum.
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        case sum == target:
            return []int{left + 1, right + 1}

        // Sum too small: the left value is the weakest link. Move left up to a
        // bigger number, which is the only way to increase the sum.
        case sum < target:
            left++
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        case sum == target:
            return []int{left + 1, right + 1}

        // Sum too small: the left value is the weakest link. Move left up to a
        // bigger number, which is the only way to increase the sum.
        case sum < target:
            left++

        // Otherwise sum is too big: move right down to a smaller number to
        // decrease it.
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        case sum == target:
            return []int{left + 1, right + 1}

        // Sum too small: the left value is the weakest link. Move left up to a
        // bigger number, which is the only way to increase the sum.
        case sum < target:
            left++

        // Otherwise sum is too big: move right down to a smaller number to
        // decrease it.
        default:
            right--
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Sorted input: the smallest sum is the leftmost pair, the largest is the
    // rightmost. Start at the widest pair and squeeze inward.
    left, right := 0, len(numbers)-1

    // Each step, evaluate the current pair and compare its sum to target.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Three outcomes, decided by how sum compares to target.
        switch {
        // Exact match — we're done. The problem is 1-indexed, so add 1 to each.
        case sum == target:
            return []int{left + 1, right + 1}

        // Sum too small: the left value is the weakest link. Move left up to a
        // bigger number, which is the only way to increase the sum.
        case sum < target:
            left++

        // Otherwise sum is too big: move right down to a smaller number to
        // decrease it.
        default:
            right--
        }
    }

    return nil // the problem guarantees exactly one solution
}
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
        if s == target:
            return [left + 1, right + 1]
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
        if s == target:
            return [left + 1, right + 1]

        # Too small: the left value is the weakest link. Move left up to a bigger
        # number, the only way to increase the sum.
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
        if s == target:
            return [left + 1, right + 1]

        # Too small: the left value is the weakest link. Move left up to a bigger
        # number, the only way to increase the sum.
        elif s < target:
            left += 1
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
        if s == target:
            return [left + 1, right + 1]

        # Too small: the left value is the weakest link. Move left up to a bigger
        # number, the only way to increase the sum.
        elif s < target:
            left += 1

        # Otherwise too big: move right down to a smaller number to decrease it.
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
        if s == target:
            return [left + 1, right + 1]

        # Too small: the left value is the weakest link. Move left up to a bigger
        # number, the only way to increase the sum.
        elif s < target:
            left += 1

        # Otherwise too big: move right down to a smaller number to decrease it.
        else:
            right -= 1
def two_sum(numbers, target):
    # Sorted input: the smallest sum is the leftmost pair, the largest is the
    # rightmost. Start at the widest pair and squeeze inward.
    left, right = 0, len(numbers) - 1

    # Each step, evaluate the current pair and compare its sum to target.
    while left < right:
        s = numbers[left] + numbers[right]

        # Exact match — done. The problem is 1-indexed, so add 1 to each.
        if s == target:
            return [left + 1, right + 1]

        # Too small: the left value is the weakest link. Move left up to a bigger
        # number, the only way to increase the sum.
        elif s < target:
            left += 1

        # Otherwise too big: move right down to a smaller number to decrease it.
        else:
            right -= 1

    return []  # the problem guarantees exactly one solution
function twoSum(numbers, target) {

}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
    // Otherwise too big: move right down to a smaller number to decrease it.
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
    // Otherwise too big: move right down to a smaller number to decrease it.
    else right--;
  }
}
function twoSum(numbers, target) {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
    // Otherwise too big: move right down to a smaller number to decrease it.
    else right--;
  }

  return []; // the problem guarantees exactly one solution
}
function twoSum(numbers: number[], target: number): number[] {

}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];
  }
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
  }
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
  }
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
  }
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
  }
}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
    // Otherwise too big: move right down to a smaller number to decrease it.
  }

}
function twoSum(numbers: number[], target: number): number[] {
  // Sorted input: start at the widest pair (smallest and largest) and squeeze in.
  let left = 0, right = numbers.length - 1;

  // Each step, evaluate the current pair and compare its sum to target.
  while (left < right) {
    const sum = numbers[left] + numbers[right];

    // Exact match — done. The problem is 1-indexed, so add 1 to each.
    if (sum === target) return [left + 1, right + 1];
    // Too small: move left up to a bigger number to increase the sum.
    else if (sum < target) left++;
    // Otherwise too big: move right down to a smaller number to decrease it.
    else right--;
  }

  return []; // the problem guarantees exactly one solution
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {

    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];
        }
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
        }
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new int[]{left + 1, right + 1};
        }
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new int[]{left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
        }
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new int[]{left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
        }
    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new int[]{left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
        }

    }
}
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new int[]{left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
            else right--;
        }

        return new int[0]; // the problem guarantees exactly one solution
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {

    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];
        }
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
        }
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new[] { left + 1, right + 1 };
        }
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new[] { left + 1, right + 1 };
            // Too small: move left up to a bigger number to increase the sum.
        }
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new[] { left + 1, right + 1 };
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
        }
    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new[] { left + 1, right + 1 };
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
        }

    }
}
public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = numbers.Length - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return new[] { left + 1, right + 1 };
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
            else right--;
        }

        return new int[0]; // the problem guarantees exactly one solution
    }
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {

}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];
    }
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];

        // Exact match — allocate the two 1-indexed positions and report the size.
    }
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];

        // Exact match — allocate the two 1-indexed positions and report the size.
        if (sum == target) {
            int* res = malloc(2 * sizeof(int));
            res[0] = left + 1;
            res[1] = right + 1;
            *returnSize = 2;
            return res;
        }
    }
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];

        // Exact match — allocate the two 1-indexed positions and report the size.
        if (sum == target) {
            int* res = malloc(2 * sizeof(int));
            res[0] = left + 1;
            res[1] = right + 1;
            *returnSize = 2;
            return res;
            // Too small: move left up to a bigger number to increase the sum.
        }
    }
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];

        // Exact match — allocate the two 1-indexed positions and report the size.
        if (sum == target) {
            int* res = malloc(2 * sizeof(int));
            res[0] = left + 1;
            res[1] = right + 1;
            *returnSize = 2;
            return res;
        } else if (sum < target) {
            // Too small: move left up to a bigger number to increase the sum.
            left++;
        }
    }
}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];

        // Exact match — allocate the two 1-indexed positions and report the size.
        if (sum == target) {
            int* res = malloc(2 * sizeof(int));
            res[0] = left + 1;
            res[1] = right + 1;
            *returnSize = 2;
            return res;
        } else if (sum < target) {
            // Too small: move left up to a bigger number to increase the sum.
            left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
        }
    }

}
#include <stdlib.h>

// Returns a malloc'd array of two 1-based indices; sets *returnSize.
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    // Sorted input: start at the widest pair and squeeze inward.
    int left = 0, right = numbersSize - 1;

    // Each step, evaluate the current pair and compare its sum to target.
    while (left < right) {
        int sum = numbers[left] + numbers[right];

        // Exact match — allocate the two 1-indexed positions and report the size.
        if (sum == target) {
            int* res = malloc(2 * sizeof(int));
            res[0] = left + 1;
            res[1] = right + 1;
            *returnSize = 2;
            return res;
        } else if (sum < target) {
            // Too small: move left up to a bigger number to increase the sum.
            left++;
        } else {
            // Otherwise too big: move right down to a smaller number to decrease it.
            right--;
        }
    }

    *returnSize = 0; // the problem guarantees exactly one solution
    return NULL;
}
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {

    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return {left + 1, right + 1};
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return {left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return {left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return {left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
        }

    }
};
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        // Sorted input: start at the widest pair and squeeze inward.
        int left = 0, right = (int)numbers.size() - 1;

        // Each step, evaluate the current pair and compare its sum to target.
        while (left < right) {
            int sum = numbers[left] + numbers[right];

            // Exact match — done. The problem is 1-indexed, so add 1 to each.
            if (sum == target) return {left + 1, right + 1};
            // Too small: move left up to a bigger number to increase the sum.
            else if (sum < target) left++;
            // Otherwise too big: move right down to a smaller number to decrease it.
            else right--;
        }

        return {}; // the problem guarantees exactly one solution
    }
};

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.