3. Two Sum
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.
Code
An integer array named
result
is initialized with a size of 2. This array will eventually hold the indices of the two numbers that sum to the target.The method uses a nested loop to iterate through the
nums
array:The outer loop (
index
) goes through each element of the array.The inner loop (
innerIndex
) also goes through the entire array.
Inside the inner loop, the code checks two conditions:
index != innerIndex
: Ensures that the same element is not used twice.(nums[index] + nums[innerIndex] == target)
: Checks if the sum of the two elements at the current indices equals thetarget
.
If a valid pair is found, the indices of these elements are stored in the
result
array. The method then immediately returns theresult
array, exiting the method.If the loops complete without finding any valid pairs, the method returns the
result
array, which would contain the default values (both indices as0
), since it was initialized with no specific values.
This approach uses a brute-force method with a nested loop to check every possible pair of elements in the nums
array to see if they sum to the target
.
The above solution when executed took 119ms of time to be executed. While this is straightforward, it has a time complexity of O(n²), where n is the length of the nums
array. This means it can be inefficient for large arrays. To enhance efficiency, a more optimal solution can be achieved using a HashMap
, allowing for a time complexity of O(n). The HashMap
would store each number and its index, allowing the program to find the complement of the current number (i.e., target - nums[index]
) in constant time.
Code: Using Hashmap
A
HashMap
callednumberMap
is created. This map will store numbers from thenums
array as keys and their corresponding indices as values.A loop iterates through each element in the
nums
array. For each number (nums[index]
), the code calculates thedifference
astarget - nums[index]
. This represents the value needed to reach the target when added to the current number.The method checks if
difference
is already a key innumberMap
. If it is found, it means that there exists a number previously encountered that, when added to the current number (nums[index]
), equals thetarget
. The method then returns an array containing:The index of the number that matches the difference (
numberMap.get(difference)
).The current index (
index
).
If the difference is not found in the map, the current number and its index are added to
numberMap
. This allows the program to keep track of previously encountered numbers for future lookups.If the loop completes without finding any valid pairs, the method returns an empty array. This indicates that no two indices were found that satisfy the condition.
Last updated