Bifalgorithm post

Let's solve the classic Amazon interview question known as twosome2. And to do this, we'll use the classic two pointers algorithm. Suppose we're given an array of values as well as a target sum. We are looking for the two numbers in the array that sum up to that target. Now on the surface, this may sound like a problem called twosome, but we're actually solving twosome2, meaning the array is sorted. Now, whenever we have a sorted array and we're searching for a target, you should definitely consider doing ABS, binary search, but we need two numbers. So that's not really going to work. The next thing you should try is a TP, not toilet paper, two pointer approach where we have two pointers at the beginning and the end. Now, if L is two and ours 15, our current sum is 17, which is too small. And since the array is sorted, we can guarantee to make it bigger by moving L over to the right. But now our sum is too big at 22. We can guarantee to make it smaller by moving R over to the middle. That is when we find our two values of 7 and 11. And if our indexes are ever equal, then we didn't find a solution. And here's my Python code. Follow me for more.

Let's solve the classic Amazon interview question known as twosome2. And to do this, we'll use the classic two pointers algorithm. Suppose we're given an array…

Ideas in this flowchart

  • Solve Twosome2 Problem
  • Problem Description
  • Given Array and Target Sum
  • Find Two Numbers That Sum to Target
  • Sorted Array
  • Algorithm Choice
  • Binary Search Not Suitable
  • Two Pointers Approach
  • Two Pointers Setup
  • Pointers at Start and End
  • Adjust Pointers Based on Sum
  • Sum Too Small: Move Left Pointer Right
  • Sum Too Big: Move Right Pointer Left
  • Solution Found: 7 and 11
  • No Solution if Pointers Meet
  • Python Code
Feed/

Let's solve the classic Amazon interview question known as twosome2. And to do this, we'll use the classic two pointers algorithm. Suppose we're given an array of values as well as a target sum. We are looking for the two numbers in the array that sum up to that target. Now on the surface, this may sound like a problem called twosome, but we're actually solving twosome2, meaning the array is sorted. Now, whenever we have a sorted array and we're searching for a target, you should definitely consider doing ABS, binary search, but we need two numbers. So that's not really going to work. The next thing you should try is a TP, not toilet paper, two pointer approach where we have two pointers at the beginning and the end. Now, if L is two and ours 15, our current sum is 17, which is too small. And since the array is sorted, we can guarantee to make it bigger by moving L over to the right. But now our sum is too big at 22. We can guarantee to make it smaller by moving R over to the middle. That is when we find our two values of 7 and 11. And if our indexes are ever equal, then we didn't find a solution. And here's my Python code. Follow me for more.

"Let's solve the classic Amazon interview question known as twosome2"

👍
0
0 comments0 shares

Sign in to view and post comments.