### Radix Sort Algorithm

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

July 08, 2019

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 would have exactly one solution, and you may not use the same element twice.

Example:

```
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
```

Simply iterate over array 2 times, and look if the sum equals to target.

```
public int[] twoSum(int[] nums, int target) {
int l = nums.length;
for (int i=0; i<l; i++) {
for (int j=i+1; j<l; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return null;
}
```

Whenever this kind of problem comes, one thing came in mind is using HashSet. But, this problem is not asking you to return the values that sum upto a target value. It is asking you to return indexes of numbers.

So, you have to maintain a *HashMap<Integer, Integer>*, which Key is the number itself, and value is its index in the original array.

HashMap has the property of searching its key and value in *O(1)*.

While iterating, we just have to check whether HashMap has the remaining difference. If its there, its a match. Else, keep a copy of this value and index in HashMap.

For this purpose, we are keeping a copy of array values and its index into a HashMap.

```
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashMap = new HashMap<>();
int l = nums.length;
for (int i=0; i<l; i++) {
int pairValue = target-nums[i];
if (hashMap.containsKey(pairValue)) {
return new int[]{i, hashMap.get(pairValue)};
}
if (!hashMap.containsKey(nums[i])) {
hashMap.put(nums[i], i);
}
}
return null;
}
```

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

Problem Implement an algorithm to determine if a string has all the characters…

A Binary tree is a data structure which has two children nodes attached to it…

Problem Statement Given a linked list, remove the n-th node from the end of list…

Problem Statement Given an array nums of n integers and an integer target, find…

Introduction Lets assume we have a csv something similar to following: Python…

Introduction In last post, we saw How to read CSV with Headers into Dictionary…

Introduction Power BI is a wonderful tool to give life to your data, by…

Problem Statement You are given an array prices where prices[i] is the price of…

Introduction In this post we will see: How to prepare a docker image for your…

Introduction We have a page, for example a . And, upon submission we would want…