Given an array of integers, return **indices** of the two numbers such that they add up to a specific **target**.

You may assume that each input will have at most one solution, and you may not use the **same index** twice.

In case no solution exists, return **[-1, -1]**

**Example:**

Input:nums=`[2, 7, 11, 15]`

,target= 9Output:[0, 1]Explanation:nums[0] + nums[1] = 2 + 7 = 9

Your algorithm should run in **O(n log n)** time and use **O(n)** extra space.

The core challenge of this problem is to find two distinct indices in the array such that the sum of the elements at these indices equals the target value. This problem is significant in various applications such as financial analysis, where you might need to find two transactions that sum up to a specific amount.

Potential pitfalls include assuming that there are multiple solutions or using the same index twice, which is not allowed.

To solve this problem, we can use a hash map to store the difference between the target and each element as we iterate through the array. This allows us to check in constant time whether the complement of the current element exists in the hash map.

Here is a step-by-step approach:

- Initialize an empty hash map.
- Iterate through the array.
- For each element, calculate its complement (target - current element).
- Check if the complement exists in the hash map.
- If it exists, return the indices of the current element and the complement.
- If it does not exist, add the current element and its index to the hash map.
- If no solution is found by the end of the iteration, return [-1, -1].

Let's break down the algorithm step-by-step:

- Create a hash map to store the value and its index.
- Iterate through the array using a for loop.
- For each element, calculate the complement (target - current element).
- Check if the complement is already in the hash map.
- If it is, return the indices of the current element and the complement.
- If it is not, add the current element and its index to the hash map.
- If no solution is found, return [-1, -1].

```
import java.util.HashMap;
public class TwoSumIII {
public int[] twoSum(int[] nums, int target) {
// Create a hash map to store the value and its index
HashMap<Integer, Integer> map = new HashMap<>();
// Iterate through the array
for (int i = 0; i < nums.length; i++) {
// Calculate the complement
int complement = target - nums[i];
// Check if the complement is in the hash map
if (map.containsKey(complement)) {
// Return the indices of the current element and the complement
return new int[] { map.get(complement), i };
}
// Add the current element and its index to the hash map
map.put(nums[i], i);
}
// If no solution is found, return [-1, -1]
return new int[] { -1, -1 };
}
}
```

The time complexity of this approach is O(n) because we are iterating through the array once. The space complexity is O(n) because we are storing each element in the hash map.

Potential edge cases include:

- An empty array: The function should return [-1, -1].
- An array with one element: The function should return [-1, -1].
- Negative numbers: The function should handle negative numbers correctly.

To test the solution comprehensively, consider the following test cases:

- Simple case: nums = [2, 7, 11, 15], target = 9, expected output = [0, 1]
- No solution: nums = [1, 2, 3], target = 6, expected output = [-1, -1]
- Negative numbers: nums = [-1, -2, -3, -4], target = -6, expected output = [1, 3]

When approaching such problems, it is essential to:

- Understand the problem requirements and constraints.
- Think about different approaches and their trade-offs.
- Start with a simple solution and then optimize it.
- Test your solution with various test cases, including edge cases.

In this blog post, we discussed the Two Sum III problem, explored different approaches to solve it, and provided a detailed solution in Java. Understanding and solving such problems is crucial for developing strong problem-solving skills. Keep practicing and exploring further to improve your skills.

For further reading and practice problems, consider the following resources: