LeetCode – Two Sum II
Table of Contents
Hey there! Today, let’s dive back into the world of LeetCode problems with Swift.
After successfully conquering the Two Sum problem, we’re geared up to take on the next challenge: Two Sum II.
To make things interesting, let’s explore this problem using a two pointers technique. Ready to tackle it together?
Problem#
We are given an array of numbers sorted in non-decreasing order (this means each element is greater or equal to the preceding one) and a target value.
We are asked to find two numbers in the array such that the sum of them is equal to the target value and return those indices.
Here is an example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Note: The index count specified in the problem starts at 1, not 0.
In this case, the values at indices 1 and 2 sum up to 9. Therefore, the expected output is the array [1,2]
representing these indices.
Note: There is always exactly one solution, elements cannot be repeated, and the solution must be achieved using constant extra space.
Solution#
For my solution, I opted to leverage the ordered nature of the list by employing a two-pointer approach. With the assurance that the list is already sorted, I establish two pointers. These pointers dynamically adjust based on whether the sum of the elements they point to is smaller or greater than the target. This strategy proves effective in navigating the sorted array efficiently.
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
var left = 0 // pointer at the beginning of the list
var right = numbers.count - 1 // pointer at the end of the list
// iterating till both pointers are side by side
while left < right {
let sum = numbers[left] + numbers[right]
if sum > target {
// since sum is bigger than target we need to make the sum smaller
right -= 1
} else if sum < target {
// since sum is smaller than target we need to make the sum bigger
left += 1
} else {
// we found the two indices, return them
return [left + 1, right + 1]
}
}
// no indices found, return nothing
return []
}
This code efficiently identifies the two indices by employing two pointers initialized at the end of the list. Leveraging the sorted nature of the list, the pointers are strategically moved as needed until a valid solution is found.
The code is well-commented to elucidate each step, and given its straightforward nature, additional explanation may not be necessary.
Conclusion#
Wrapping up our exploration of the Two Sum II problem in Swift, we utilized a handy technique known as ’two pointers.’ The inherent order of the list allowed us to smartly navigate through elements, efficiently narrowing down potential pairs that add up to the target value.
Swift’s simplicity and expressiveness played well with this approach, allowing us to implement a clean and concise solution. The language’s array handling, combined with the straightforward syntax, offered a seamless experience in dealing with the intricacies of the problem.
No need for any Swift promotional banner here—just appreciating how the language’s features naturally complemented our strategy. Swift or not, exploring these algorithmic challenges keeps our coding skills sharp and our problem-solving toolkit diverse. Ready for the next challenge?