Mediocre Electron Herding

What even is code

Two Sum

Gonna pharaphrase this from leetcode

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.

Here is the one solution I did in Java using a hashmap

class Solution {
public int[] twoSum(int[] nums, int target)
{
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++)
{
//Does the compliment of the target exist
if(map.containsKey(target - nums[i]) && map.get(target - nums[i]) != i)
return new int[]{map.get(target - nums[i]), i};
else
map.put(nums[i], i);
}
return null;
}
}

Another solution for this is to instead of tracking the compliments of the numbers we come accross, we can simply fill the map in one loop, and then scan the map in the next loop.

There is also a brute force solution where you roll through both arrays in a nested loop but its dumb and I am not going to bother with it.