If you have been asked to find a solution for two sum problem in Java on the technical interview, so you need to follow the following steps. Here are several moments you should keep in mind. First of all, you should know that we will use a map collection to hold temporary values. Second, the main idea is to subtract each element of an array list from the given sum. For example, if the sum is equal to 7, and the current element equals 5. To get a total 7 we need to make 5 + 2, as a result, we have to check if the element with the value of 2 exists. Based on this logic, we could find two elements from the array list that we can add to each other and get a given number.
Two sum problem (Java). Step-by-step implementation.
Detailed solution for two sum problem:
- Set up an empty array of two elements where we can save the final result. That will contain indexes of two values from an array;

//Created a new array of integers
int[] intArray = new int[]{2,4,5,11,21};
//Sum value we are looking for
int lookingForValue = 7;
//Array with result final values
int[] resultArray = new int[2];
- Set up the map to hold temporary values that have been checked in the loop. Why do we use the map? We use a map because it provides the “containsKey” method. So that might easily help us to find the needed value. It is easier and faster to find the value by a key in a map than searching in an array list;

//Creating a map to save a temporary values to find needed result
Map<Integer, Integer> map = new HashMap<>();
- Start a loop to go through an array and check each element;
//Going through the array list
for (int i = 0; i < intArray.length; i++)
- Here is the main trick. It doesn’t look straightforward. On this spot, our program has two values. The first value is the given sum, which is 7. The second value is the value of the current element, which is 2. Based on that, we have to check if we have 5 in our array. To avoid going through each element in an array, we are calling map.containsKey(7-2) method;

//Checking if hashmap already has the value we check
if (!map.containsKey(lookingForValue - intArray[i]))
- We should put all the checked elements on the map to use them to compare with the next elements. The key will be the value. The value will be key. We are doing so because we will use map.containsKey method to search;

//Checking if hashmap already has the value we check
if (!map.containsKey(lookingForValue - intArray[i])){
//We are saving as value as a key / index as a value
map.put(intArray[i], i);
- If the result of the sum value minus the current element value is in the map, that means we have found two elements. For instance, if the current element equal to 5, the sum value is 7. And after the search, we figured out that the element equal to 2 is already on the map—means we have found a solution because 2+5 =7. And we can add indexes of 2 and 5 in an array result;

resultArray[1] = i;
resultArray[0] = map.get(lookingForValue - intArray[i]);
All steps schema:






Full code solution:
private void twoSumProblem() {
//Created a new array of integers
int[] intArray = new int[]{2,4,5,11,21};
//Sum value we are looking for
int lookingForValue = 7;
//Array with result final values
int[] resultArray = new int[2];
//Creating a map to save a temporary values to find needed result
Map<Integer, Integer> map = new HashMap<>();
//Going through the array list
for (int i = 0; i < intArray.length; i++) {
//Checking if hashmap already has the value we check
if (!map.containsKey(lookingForValue - intArray[i])) {
//We are saving as value as a key / index as a value
map.put(intArray[i], i);
} else {
resultArray[1] = i;
resultArray[0] = map.get(lookingForValue - intArray[i]);
}
}
//Showing in logs the result
Log.d("arrayList", "result " + intArray[resultArray[0]] + " and " + intArray[resultArray[1]]);
}
In this article, I show you the solution for two sum problem (Java0.
Full code and more examples in my facebook group.
Useful links:
Leave Any Questions Related To This Article Below!