Write an algorithm to solve this problem
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
Write an algorithm to solve this problem
Initial Attempt:
The outer loop will iterate over indices of the first number in the array.
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.
firstNum to the element of nums at firstIdx.
firstIdx + 1 up to the length of nums minus one. This is our secondIdx.
secondNum to the element of nums at secondIdx.
firstNum plus secondNum is equal to 10, return these numbers in an array, with firstNum first and secondNum second.
Return null.
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.
0 and 1. The sum of these numbers (6 and 3) is not equal to 10, so we continue.

secondIdx to 2. The sum of numbers at indices 0 and 2, once again, is not equal to 10.

secondIdx is 3, the second number is 7, and the sum of the first and second numbers is still not 10.

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.

1 and 2. The sum of these numbers (3 and 8) is not equal to 10 so we continue.

1 and 3. The sum of these numbers (3 and 7) is 10 so we return this pair as the result array.

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.
0 and 1. The sum of these numbers (1 and 2) is not equal to 10 so we continue.

secondIdx to 2. The sum of numbers at indices 0 and 2, once again, is not equal to 10.

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.

1 and 2. The sum of these numbers (2 and 5) is not equal to 10 so we continue.

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.

5 and 6. Their sum is not equal to 10.

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 .
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 .