Two Sum with Sorted Array

Опубликовано: 07 Октябрь 2025
на канале: Kenny Yip Coding
558
9

Given a sorted array of integers and a target integer number, if there are two distinct numbers in the sorted list that can add up to the target, return true otherwise, false. This problem is similar to leetcode 167 two sum II input array is sorted.

https://leetcode.com/problems/two-sum...

In this video, we started off with a brute force solution by checking every single pair combination. This is considered brute force because there is work being done that isn't necessary. This solution would be O(N^2).

Instead, we can use the two pointer technique to traverse through the list in a single loop for a runtime of O(N). One variable keeps track of the left index (smallest number) while the another keeps track of the right index (largest number). If the sum of the number at these indices is too small, move left up, if too big, move right down.

Python Playlist:
   • Python Data Structures and Algorithms  

Github: https://github.com/ImKennyYip/python-...

Subscribe for more coding tutorials 😄!

⭐ If you enjoyed the tutorial and would like to support the channel, you can do so here 💖: https://buymeacoffee.com/kennyyipcoding