DEV Community

Cover image for The Two Sum Problem -Java edition
Perry H
Perry H

Posted on

3 1

The Two Sum Problem -Java edition

This is a first post in a series on solutions written in Java to some beginner common code challenge questions. The solutions written below are not the only solutions. If you have another solution, please leave it in the comments! Today, I am going to tackle the Two Sum problem.

The Problem

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

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

The solution - brute force/naive

When I first encountered this problem, my initial solution had two loops.

public class TwoSum {

    // Solution
    public static int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int x = i + 1; x < nums.length; x++) {
                if (nums[i] + nums[x] == target) {
                    return new int[] { i, x };
                }
            }
        }
        return new int[] {};
    }

    // Test the solution
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter Array length");

        int n = keyboard.nextInt();
        int[] nums = new int[n];
        System.out.println("Enter Array items");

        for(int i = 0; i <n; i++) {
            nums[i] = keyboard.nextInt();
        }

        System.out.println("Enter Target");
        int target = keyboard.nextInt();
        keyboard.close();

        int [] solution = twoSum(nums, target);

        if(solution.length == 2) {
            System.out.println("Solution is: " + solution[0] + ", " + solution[1]);

        }
        else {
            System.out.println("No solution");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Example program output:

Enter Array length
4
Enter Array items
2
7
11
15
Enter Target
17
Solution is: 0 3

Enter fullscreen mode Exit fullscreen mode

This works and will solve the problem, but its Big O complexity for time is O(n^2). Why is this? Because in the worst case scenario, the solution has to loop through the list twice. So as the list grows, the solution grows at an exponential rate. The Space complexity is O(1). It is constant because the algorithm does not rely on any additional data structures. See here for more on Big O complexity.
We can do better. Anytime I see a nested loop, it is a flag that causes me to pause and think, is there a more efficient way? Maybe we can only loop through it once.

A Better Approach

    // Solution
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> numMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int dif = target - nums[i];
            if (numMap.get(dif) != null) {
                return new int[] { i, numMap.get(dif) };
            } else {
                numMap.put(nums[i], i);
            }
        }
        return new int[] {};
    }

// Test code remains the same
Enter fullscreen mode Exit fullscreen mode

This solution obviously gives us a better outcome in Big O time complexity because there is only one loop. No matter how big our inputs get, the worst case will only run through the entire loop once. This gives O(n) (linear) for Big O time complexity. However, we do include a Hash Table data structure in this algorithm, which could be as large as the array input. This gives us the space complexity of O(n). This would be an example of a space-time tradeoff. I personally like the clarity of using the one loop and the Hash Table.

I have found that when running into code that has nested loops, a good question to ask is: "Can I use a dictionary or a map here?" What are some of your tips you have used to solve these types of problems?

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay