LeetCode 167. Two Sum II - Input Array Is Sorted || Java Solution Walkthrough

preview_player
Показать описание
LeetCode is a great platform for people who want general coding practice, whether that be for interviews or just wanting to practice their problem solving. I will be taking you through all the steps of finding the optimal solution to each problem on the site following a logical ordering setout by LeetCode themselves.

In this video, we are going over the problem 167. Two Sum II - Input Array Is Sorted and the goal is to search for exactly two numbers in this array which add up to a target number. Once we find those numbers, we need to remember the indexes we found them at, add one to make it indexed by 1, and return them in the correct order in a new array.

This problem is marked as a medium, and I believe its because of the wording. Once you know the trick this problem is actually easier than the first Two Sum problem. The magic in this problem lies in the fact that the array is already sorted for us, meaning the smallest numbers are starting at 0 and the largest numbers are ending at the last index. There is a searching algorithm that works very well and quick on pre-sorted arrays called binary search. We can basically, take the first number of the array, and the number in the middle of the array and determine if the resulting number is too large or too small, and slide the pointers in either direction since we know numbers to the left are smaller, and numbers to the right are larger.

~~~ Stay Up To Date With My Social Media ~~~

#unknownkoder #leetcode #algorithms
Рекомендации по теме
join shbcf.ru