Two Sum with Sorted Array
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-ii-input-array-is-sorted/
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:
https://youtube.com/playlist?list=PLnKe36F30Y4bcRomKi02sP9NR27KnBqCK
Github: https://github.com/ImKennyYip/python-data-structures-algorithms
Subscribe for more coding tutorials 😄!
Other Videos By Kenny Yip Coding
2024-02-08 | For Each Loop in C++ |
2024-01-30 | Code Flappy Bird in Java |
2024-01-29 | For Loops in C++ |
2024-01-18 | Why does index start at 0 in computer science? |
2024-01-17 | C++ Vectors and Dynamic Arrays |
2024-01-16 | C++ String and C Strings |
2024-01-08 | Arrays in C++ |
2023-11-02 | Binary Search in Python |
2023-10-30 | Space Complexity of Algorithms |
2023-10-29 | Linear Search in Python |
2023-10-26 | Two Sum with Sorted Array |
2023-10-13 | How to set up C++ in Visual Studio Code |
2023-10-09 | Code Snake Game in Python |
2023-09-19 | Code a game in Java |
2023-08-14 | Code Minesweeper in Java |
2023-08-02 | Code Black Jack in Java |
2023-07-19 | Code Snake Game in Java |
2023-07-16 | Code Tic Tac Toe in Java |
2023-06-08 | Memory Card Game in Javascript |
2023-05-29 | How to set up Java in Visual Studio Code |
2023-05-21 | Code Doodle Jump in Javascript |