Two Sum Problem - Leetcode - 1 - Java Solution

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

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

Solution :

[class Solution {

    public int[] twoSum(int[] nums, int target) {

        int[] output = new int[2];

        int n = nums.length;

        HashMap<Integer, Integer> freq = new HashMap<>();

        for (int i = 0; i < n; i++){

            freq.put(nums[i],i);

        }

        System.out.println(freq);

        for (int i = 0; i < n; i++){

            int b = target - nums[i];

            if (freq.containsKey(b) && freq.get(b) != i){

                output[0] = freq.get(b);

                output[1] = i;

                return output;

            }

        }

        return output;

    }

}

]

Explanation : 

We solve this problem by an optimised approach using O (N) time complexity. The questions tell us that we have to return an array of two numbers where array[num1] + array[num2] == target. 

Also num1 != num2. We first create a hashmap with integer and their indexes. Then we assume that we already have num1. 

To get num2 the formula becomes array[num2] = target - array[num1]. Now we use this formula and iterate over the array. 

Now we assume num1 as i, get the value of num2 and search the hashmap for the value of num2. If we find the value of num2 in hashmap, we get its index and add it to the output array with index i and return the array.  

Let me know what you think in comments below. 

COMMENTS

BLOGGER
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: Two Sum Problem - Leetcode - 1 - Java Solution
Two Sum Problem - Leetcode - 1 - Java Solution
Given an array of integer nums and an integer target, return indices of the two numbers such that they add up to the target.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRTA-goS1Zz0Ekf192AamoNKGJCeMVYxxXTuRf1bvQrrpSBHfSB81GGdHp-7qONy4deSUu5gX52Pz9W4dXHPCVcitQfWFRZUJ2GZuJfCtRqgPZMP8zeE0ugMHzNFfj35uKFQ0wdsYqJ5S__ab8FoyvgG1NP47YNjxbY8um81txmnrrzSfLUILhzbxLVf4/w133-h200/altumcode-PNbDkQ2DDgM-unsplash.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRTA-goS1Zz0Ekf192AamoNKGJCeMVYxxXTuRf1bvQrrpSBHfSB81GGdHp-7qONy4deSUu5gX52Pz9W4dXHPCVcitQfWFRZUJ2GZuJfCtRqgPZMP8zeE0ugMHzNFfj35uKFQ0wdsYqJ5S__ab8FoyvgG1NP47YNjxbY8um81txmnrrzSfLUILhzbxLVf4/s72-w133-c-h200/altumcode-PNbDkQ2DDgM-unsplash.jpg
A Passionate Developers Creation
https://www.apdevc.com/2024/10/two-sum-problem-leetcode-1-java-solution.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2024/10/two-sum-problem-leetcode-1-java-solution.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content