GitHub Repository Submission

When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more

Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.

Exercise 3

Write an algorithm to solve this problem

Solution

Initial Attempt:

  • Use nested loops.
  • The outer loop will iterate over indices of the first number in the array.

    • Within each outer loop iteration, a second loop will iterate over the indices of the second number.
      • Sum the current first and second numbers.
      • If the sum equals 10, return an array containing the pair. The first number should be the first element in the array, and the second number should be the second element.
  • If the outer loop completes without finding a correct pair, return null.


While this algorithm might look complete, it still lacks precision. What does it mean when we say we'll iterate over indices of the first number? In this case, it means iterating from 0 until the second to last index of our input array, or the length of our input minus two. This ensures the second-to-last number in the nums array can still be the first number in a potential result pair.

The inner loop is trickier. Where should it start? Starting from 0 could lead to the index of the first number being greater than the index of the second number, violating the order requirement. For example, in the array [1, 3], if the first index is 1 and the second is 0, the pair would be [3, 1], which is invalid.

We also can't start the inner loop at the same index as the outer loop because we might include the same number twice. For example, in the array [2, 3], if both indices start at 0, we could potentially get the pair [2, 2], which is also invalid.

Therefore, the index of the second number needs to start one position greater than the index of the first number (firstIdx + 1) and iterate until the end of the array, or the length of our input minus one. As the index we use for our outer loop increases, the inner loop will iterate over a smaller set of indices.

Here's a more precise algorithm that encompases these details we've discussed:

Final Algorithm:

  • Create a loop to iterate from 0 up to the length of the input array, nums, minus two (The second to last index). This is our firstIdx.

    • Initialize a variable firstNum to the element of nums at firstIdx.
    • Create a loop to iterate from firstIdx + 1 up to the length of nums minus one. This is our secondIdx.
      • Initialize a variable secondNum to the element of nums at secondIdx.
      • If firstNum plus secondNum is equal to 10, return these numbers in an array, with firstNum first and secondNum second.
  • Return null.


Walkthrough I

Let's go through the example array [6, 3, 8, 7]. The target sum is 10 per the problem description.

The First Outer Loop Iteration

Beginning with the first number of the array at index 0, 6, we first find all possible pairs that can be created with this number as the first number. As you can see, we're showing the inner loop at work here. The inner loop, in this case, starts with an index of 1 and ends with an index of 3.

  • First, we get a pair of numbers at indices 0 and 1. The sum of these numbers (6 and 3) is not equal to 10, so we continue.

  • The first number remains the same, but we increment the secondIdx to 2. The sum of numbers at indices 0 and 2, once again, is not equal to 10.

  • In the last inner iteration, the secondIdx is 3, the second number is 7, and the sum of the first and second numbers is still not 10.

  • Our secondIdx would now be 4, but as our algorithm stated, our inner loop will only iterate up the length of our input minus one. [6, 3, 8, 7].length - 1 is 3. As such, our inner loop has finished for this iteration of our outer loop, where our firstIdx is 0.

The Second Outer Loop Iteration

Next, we need to check the pairs of numbers where the first number starts at index 1. The inner loop, in this case, starts with an index of 2 and ends with an index of 3.

  • First, we get a pair of numbers at indices 1 and 2. The sum of these numbers (3 and 8) is not equal to 10 so we continue.

  • Finally, we get a pair of numbers at indices 1 and 3. The sum of these numbers (3 and 7) is 10 so we return this pair as the result array.


Walkthrough II

Let's see another example where we don't find a solution pair, with the array [1, 2, 5, 6].

The First Outer Loop Iteration

Beginning with the first number of the array at index 0, 1, we first find all possible pairs that can be created with this number as the first number. Again, our diagram shows the inner loop's progression. Since our array is the same length as the previous example, this inner loop will again iterate from index 1 up to index 3.

  • First, we get a pair of numbers at indices 0 and 1. The sum of these numbers (1 and 2) is not equal to 10 so we continue.

  • The first number remains the same, but we increment the secondIdx to 2. The sum of numbers at indices 0 and 2, once again, is not equal to 10.

  • In the last inner iteration, the secondIdx is 3, the second number is 6, and the sum of the first and second number is still not 10.


The Second Outer Loop Iteration

Next, we need to check the pairs of numbers where the first number starts at index 1. Now that our firstIdx in our outer loop has increased by 1, our secondIdx within The inner loop will start with a value of 2 and end with a value of 3.

  • First, we get a pair of numbers at indices 1 and 2. The sum of these numbers (2 and 5) is not equal to 10 so we continue.

  • Next, we get a pair of numbers at indices 1 and 3. The sum of these numbers (2 and 6) is 8, so we continue.


The Third Outer Loop Iteration

Finally, we need to check the pairs of numbers where the first number starts at index 2. Remember that the inner loop will start at the firstIdx plus one, up to the last index of the input. Here, that means we're iterating from 3 to 3. In practical terms, this means our inner loop is only going to iterate a single time.

  • In our single iteration the pair of numbers is 5 and 6. Their sum is not equal to 10.

  • Our outer loop has reached its end without us finding a matching pair, and so we return null.

Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...

Submit your solution for LSBot review.

Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...