Two Sum with Sorted Array

Publié le: 26 octobre 2023
sur la chaîne: 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


Sur cette page du site, vous pouvez voir la vidéo en ligne Two Sum with Sorted Array durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Kenny Yip Coding 26 octobre 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 752 fois et il a aimé 9 téléspectateurs. Bon visionnage!