1 min to read
Two sum  Leet Code Solution
In this post, we are going to solve LeetCode “Two Sum” programming problem. You can find the original question here > Two Sum Problem
Problem Statement: Two Sum Problem
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You can find the original question here > Two Sum Problem
Explanation
We will use some optimized approach in this solution rather then the brute force. we will use dictionary to store the value and index of the array. We will iterate over the array and check if the difference of target and current value is present in the dictionary or not. If it is present then we will return the index of the current value and the index of the difference of target and current value.
Complexity Analysis

Time complexity : O(n). We traverse the list containing n elements only once. Each look up in the table costs only O(1) time.

Space complexity : O(n). The extra space required depends on the number of items stored in the hash table, which stores at most n elements.
Conclusion
In this post, we have seen how to solve LeetCode “Two Sum” programming problem. If you want to solve more such problems please visit LeetCode