LeetCode Explained: 1 Two Sum

In this question, we are given an array of integer nums and an integer target, and we are asked to return two distinct indices such that the numbers in those positions add up to be the target. The question assumes that there is one and only one solution for each input.

Approach 1: Brute Force

Traverse the array. For each number num in the array, traverse the rest of the array to see if target - num can be found.

Approach 2: Sorting

Sort the input array in ascending order, then use two pointers(starting from the left-most and right-most position) to find the two numbers that sum up to target.

Approach 3: Hash Table

In the brute force approach, we have to loop through the rest of the array again and again to find if a possible solution exists, which is very time consuming. One possible improvement is to make this process faster.

Related Questions

1679 Max Number of K-Sum Pairs

A lifelong learner who has enthusiasm for sharing knowledge; a developer who loves bringing ideas to life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store