Edit Problem
Title
Problem URL
Difficulty
Easy
Medium
Hard
Tags
Two Pointers
×
Add tag
Go file path (optional)
Notes (optional)
## Thoughts Easy Problem. The solution will only work when the array is already sorted ## Complexity Analysis **Time Complexity: O(n)** The algorithm uses two pointers starting at opposite ends of the array, moving toward each other. In the worst case (no pair found), every element is visited exactly once before `start` and `end` meet. Since each pointer only ever moves inward and never backtracks, you get at most `n` iterations total. **Space Complexity: O(1)** Only two integer variables (`start` and `end`) are used regardless of input size. The returned slice is always a fixed size of 2 elements, so it doesn't scale with input and isn't counted as auxiliary space. --- **Why this is efficient:** The naive brute-force approach of checking every pair would be O(n²) time. This algorithm achieves linear time by exploiting the fact that the array is sorted — if the current sum is too large, you know moving `end` left is the only useful action, and vice versa for `start`. This eliminates the need for a nested loop entirely.
First solved
Mar 2, 2026
Clear
Save Changes
Cancel