What do you mean by "can't output?" Could you be more specific? Did you mean the while loop didn't end? I guess you meant the output was just `[0, 0]`

?

Looks like the code wanted to only run `at most once`

to get the result. This code assumed that the input array `a[]`

is already in ascending order, so it doesn't work in the case like `a[] = [4, 2, 5, 6], target = 7`

. Then you might need to sort the array first, but the time complexity would include the sorting algorithm.

Also, the problem has been revised that the index of the solution starts with `0`

instead of `1`

so you don't need to use `index + 1`

for the solution anymore:

```
if (sum == target) {
returnSize[0] = beg;
returnSize[1] = end;
break;
}
```

return returnSize;

I checked the function/method signature `twoSum()`

in leetCode but did see `int twoSum(int a[],int n,int target,int returnSize)`

in C, C++ nor Java.

The key idea is also to run through the list `at most once`

to get the result. How to achieve that? When we iterate the array, we want to know if the current element is one of our results. To know if the `current element`

if one of our results we have to have something to store the `another_element -> current_element_index`

. Here `another element`

means `target - current_element`

. In addition to that, the time complexity to access the `another element`

and its `index`

should be least. So `hash map`

can help us achieve this goal by storing `another element`

as key and its `index`

as the value.

For instance, when iterating the array [2, 3, 4, 6] and target is 7, let's say we are dealing with 3 now, so `another element`

should be 7 - 3 = `4`

. We store the `key`

with `4`

and its `value`

with `current element = 3 index`

with `1`

. Then next time when we encounter `4`

, we can quickly know it's `another element`

that we want by `hashMap.containsKey(4)`

. Then we output element `4`

's index and `3`

's index since we know element `3`

's index is `hashMap.get(4)`

.

like the Java code below:

```
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
int[] res = new int[2];
for(int i=0;i<nums.length;i++){
if(map.containsKey(nums[i])){
res[0] = map.get(nums[i]);
res[1] = i;
break;
}else{
map.put(target-nums[i],i);
}
}
return res;
}
}
```