2.1 Pair with Target Sum (easy)
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Example 2:
Example 3:
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
Follow-up: Can you come up with an algorithm that is less than O(n2)
time complexity?
Solution:
Method 1: Brute Force
This approach is straightforward. We can check for every pair in the array and if their sum is equal to the given target, print their indices. This kind of Brute Force solution needs to check every possible pair and number of possible pairs in the array = n * (n – 1) / 2. So, in the worst-case, this approach can be slow.
Run a loop to maintain the first index of the solution in the array
Run another loop to maintain a second index of the solution for every first integer
If at any point, the sum of values of two indices is equal to the target
Print its indices
Complexity Analysis:
Time Complexity
O(N * N), where N = size of the array. As we check for possible pair, and the total number of pairs are: N * (N – 1) / 2.
Space complexity
O(1). Only constant space for variables is used.
Method 2: Using Sorting
We can get some advantage if the array is already sorted. An approach to solve the problem would be:
Sort the given array.
Start two pointers. Pointer A starts from the beginning of the array, such that it points to the smallest element. Pointer B starts from the end of the array, pointing at the maximum element of the array.
Now, start a while loop
while(pointer A < pointer B)
Get a sum of the elements at
pointerA
andpointerB
.This is where the magic comes in. If the sum is less than
target
, it simply means that we need to add a bigger number. Hence move thepointerA
one step ahead. Else, we need a smaller number, and we can move thepointerB
one step backward.Somewhere along this iteration, we will get our desired indices.
The above method works in a time complexity of O(n∗logn) because of the sorting step involved. You can use a quick sort algorithm to sort your array.
Method 3: Use Hashing
In the previous method, we did not use extra space and achieved a decent time complexity. But, if you can allow yourself to compromise on some space, you can solve this problem in an even efficient manner.
Instead of finding two numbers whose sum equal to a target
value, we can think of the problem in an alternative way:
target_value − first_number = second_number
So, we can develop an algorithm in the following way:
Initialize a hash-table that will store the index and the element.
Start to traverse the array.
For each element in the array use the above define formula to find the complementing number.
Look up the complementing number in the hash table. If found, return the
2
indices.Else, add the element along with its index to the hash table and proceed with the other elements.
Implementation:
Time Complexity: O(n) Space Complexity: O(n)
Last updated