# Two Sum — LeetCode #1

Given an array of integers `nums` and an integer `target`, return *indices of the two numbers such that they add up to* `target`.

You may assume that each input would have ***exactly* one solution**, and you may not use the *same* element twice.

You can return the answer in any order.

**Example 1:**

```plaintext
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
```

**Example 2:**

```plaintext
Input: nums = [3,2,4], target = 6
Output: [1,2]
```

**Example 3:**

```plaintext
Input: nums = [3,3], target = 6
Output: [0,1]
```

**Constraints:**

* `2 <= nums.length <= 104`
    
* `-109 <= nums[i] <= 109`
    
* `-109 <= target <= 109`
    
* **Only one valid answer exists.**
    

**Follow-up:** Can you come up with an algorithm that is less than `O(n2)` time complexity?

# **Solutions:**

**Javascript:**

To find the indices of the two numbers in the given array `nums` such that they add up to the target number `target`, you can use a brute force approach with a time complexity of O(n^2). Here's one way you can do it:

```javascript
function twoSum(nums, target) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j];
      }
    }
  }
}
```

This function loops through each element `nums[i]` in the array, and for each element it loops through all the remaining elements `nums[j]` to see if `nums[i] + nums[j]` equals the target. If it does, the function returns the indices `[i, j]`.

However, if you want an algorithm with a lower time complexity, you can use a hash map (also known as a dictionary or an object in JavaScript). This approach has a time complexity of O(n) and a space complexity of O(n). Here’s how you can implement it:

```javascript
function twoSum(nums, target) {
  const map = {};

  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map[complement] !== undefined) {
      return [map[complement], i];
    }
    map[nums[i]] = i;
  }
}
```

This function loops through each element `nums[i]` in the array and stores the complement `target - nums[i]` in a hash map. If the complement is found in the hash map, it means that `nums[i]` and the element with the complement add up to the target, so the function returns the indices `[map[complement], i]`.

**C#:**

```csharp
using System.Collections.Generic;

public int[] TwoSum(int[] nums, int target)
{
    var map = new Dictionary<int, int>();

    for (int i = 0; i < nums.Length; i++)
    {
        int complement = target - nums[i];
        if (map.ContainsKey(complement))
        {
            return new int[] { map[complement], i };
        }
        map[nums[i]] = i;
    }

    return new int[0];
}
```

**Python:**

```python
def two_sum(nums, target):
    map = {}

    for i in range(len(nums)):
        complement = target - nums[i]
        if complement in map:
            return [map[complement], i]
        map[nums[i]] = i

    return []
```

**Java:**

```java
import java.util.HashMap;
import java.util.Map;

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();

    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement)) {
            return new int[] { map.get(complement), i };
        }
        map.put(nums[i], i);
    }

    return new int[0];
}
```

**Typescript:**

```typescript
function twoSum(nums: number[], target: number): number[] {
  const map: { [key: number]: number } = {};

  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map[complement] !== undefined) {
      return [map[complement], i];
    }
    map[nums[i]] = i;
  }

  return [];
}
```

To have more solutions to LeetCode problems just visit my article here on medium [https://medium.com/@araneznorman](https://medium.com/@araneznorman)
