Two Sum with Sorted Array

Published: 26 October 2023
on channel: Kenny Yip Coding
752
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


On this page of the site you can watch the video online Two Sum with Sorted Array with a duration of hours minute second in good quality, which was uploaded by the user Kenny Yip Coding 26 October 2023, share the link with friends and acquaintances, this video has already been watched 752 times on youtube and it was liked by 9 viewers. Enjoy your viewing!