Two sum problem in Java. The best solution example. • IT Blog
IT Blog
Boris IT Expert

Two sum problem in Java. The best solution example.

Boris~November 6, 2020 /Array List

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:

//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]))
two sum problem java solution
//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);
two sum problem java solution
resultArray[1] = i;
resultArray[0] = map.get(lookingForValue - intArray[i]);

All steps schema:

two sum problem java solution
two sum problem java solution
two sum problem java solution
two sum problem java solution
two sum problem java solution
two sum problem java solution

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!