Answer :
Certainly! Let's solve the problem step-by-step.
### Problem Understanding
The problem gives an array of integers (`nums`) and an integer (`target`). Our objective is to find two distinct indices in `nums` such that the sum of the elements at those indices equals `target`. Each problem instance is guaranteed to have exactly one solution.
### Approach
We'll use a single scan through the array while keeping track of the numbers we have seen and their corresponding indices. Here's how we can approach this:
1. Use a dictionary (or hashmap):
- This will store numbers as keys and their indices as values. This allows us to quickly check if the complement of the current number (the value needed to reach `target` when added to the current number) exists in our previously seen numbers.
2. Iterate through the array:
- For each number in the array, calculate its complement: `complement = target - current_number`.
- Check if this complement is already in our dictionary.
- If it is, return the current index and the index of the complement from the dictionary as the solution.
- If it isn't, add the current number and its index to the dictionary and move to the next number.
3. Return statement:
- By the problem's guarantee, there will always be a solution, so we don't need to handle the case of no solution found.
### Examples Walkthrough
#### Example 1:
```
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
```
- Initialize an empty dictionary.
- Start iterating through `nums`:
- At index 0, number is 2. Complement = 9 - 2 = 7. 7 is not in the dictionary. Add 2 to dictionary with key-value pair {2: 0}.
- At index 1, number is 7. Complement = 9 - 7 = 2. 2 is in the dictionary with index 0. Return [0, 1].
#### Example 2:
```
Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Explanation: Because nums[1] + nums[2] == 6, we return [1, 2].
```
- Initialize an empty dictionary.
- Start iterating through `nums`:
- At index 0, number is 3. Complement = 6 - 3 = 3. 3 is not in the dictionary. Add 3 to dictionary with key-value pair {3: 0}.
- At index 1, number is 2. Complement = 6 - 2 = 4. 4 is not in the dictionary. Add 2 to dictionary with key-value pair {3: 0, 2: 1}.
- At index 2, number is 4. Complement = 6 - 4 = 2. 2 is in the dictionary with index 1. Return [1, 2].
#### Example 3:
```
Input: nums = [3, 3], target = 6
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 6, we return [0, 1].
```
- Initialize an empty dictionary.
- Start iterating through `nums`:
- At index 0, number is 3. Complement = 6 - 3 = 3. 3 is not in the dictionary. Add 3 to dictionary with key-value pair {3: 0}.
- At index 1, number is 3. Complement = 6 - 3 = 3. 3 is in the dictionary with index 0. Return [0, 1].
### Summary of Steps
1. Create an empty dictionary.
2. Iterate through the array with enumeration to access both index and value.
3. For each number, calculate its complement (target - current number).
4. Check if the complement exists in the dictionary.
- If it exists, return the index of the complement and the current index.
- If it doesn't, store the current number and its index in the dictionary.
By following these steps, you can efficiently find the indices of the two numbers that add up to the target.
### Problem Understanding
The problem gives an array of integers (`nums`) and an integer (`target`). Our objective is to find two distinct indices in `nums` such that the sum of the elements at those indices equals `target`. Each problem instance is guaranteed to have exactly one solution.
### Approach
We'll use a single scan through the array while keeping track of the numbers we have seen and their corresponding indices. Here's how we can approach this:
1. Use a dictionary (or hashmap):
- This will store numbers as keys and their indices as values. This allows us to quickly check if the complement of the current number (the value needed to reach `target` when added to the current number) exists in our previously seen numbers.
2. Iterate through the array:
- For each number in the array, calculate its complement: `complement = target - current_number`.
- Check if this complement is already in our dictionary.
- If it is, return the current index and the index of the complement from the dictionary as the solution.
- If it isn't, add the current number and its index to the dictionary and move to the next number.
3. Return statement:
- By the problem's guarantee, there will always be a solution, so we don't need to handle the case of no solution found.
### Examples Walkthrough
#### Example 1:
```
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
```
- Initialize an empty dictionary.
- Start iterating through `nums`:
- At index 0, number is 2. Complement = 9 - 2 = 7. 7 is not in the dictionary. Add 2 to dictionary with key-value pair {2: 0}.
- At index 1, number is 7. Complement = 9 - 7 = 2. 2 is in the dictionary with index 0. Return [0, 1].
#### Example 2:
```
Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Explanation: Because nums[1] + nums[2] == 6, we return [1, 2].
```
- Initialize an empty dictionary.
- Start iterating through `nums`:
- At index 0, number is 3. Complement = 6 - 3 = 3. 3 is not in the dictionary. Add 3 to dictionary with key-value pair {3: 0}.
- At index 1, number is 2. Complement = 6 - 2 = 4. 4 is not in the dictionary. Add 2 to dictionary with key-value pair {3: 0, 2: 1}.
- At index 2, number is 4. Complement = 6 - 4 = 2. 2 is in the dictionary with index 1. Return [1, 2].
#### Example 3:
```
Input: nums = [3, 3], target = 6
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 6, we return [0, 1].
```
- Initialize an empty dictionary.
- Start iterating through `nums`:
- At index 0, number is 3. Complement = 6 - 3 = 3. 3 is not in the dictionary. Add 3 to dictionary with key-value pair {3: 0}.
- At index 1, number is 3. Complement = 6 - 3 = 3. 3 is in the dictionary with index 0. Return [0, 1].
### Summary of Steps
1. Create an empty dictionary.
2. Iterate through the array with enumeration to access both index and value.
3. For each number, calculate its complement (target - current number).
4. Check if the complement exists in the dictionary.
- If it exists, return the index of the complement and the current index.
- If it doesn't, store the current number and its index in the dictionary.
By following these steps, you can efficiently find the indices of the two numbers that add up to the target.