Two sum - Leet Code Solution

Featured image

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

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