Problem:
Given an array of integers
Example:
Simple Solution (Python):
Explanation for Dummies:
Feel free to comment with your questions or share other solutions!
Given an array of integers
nums and an integer target, return indices of the two numbers such that they add up to target.Example:
nums = [2, 7, 11, 15],target = 9- Output:
[0, 1] - Explanation:
2 + 7 = 9(indices0and1)
Simple Solution (Python):
Python:
def twoSum(nums, target):
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
# Example usage:
nums = [2, 7, 11, 15]
target = 9
print(twoSum(nums, target)) # Output: [0, 1]
Explanation for Dummies:
- Look at each number in the list.
- For every number, check every other number after it to find a pair that adds up to your target number.
- If you find them, return their positions (indices) in the list.
Feel free to comment with your questions or share other solutions!