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:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6 Output: [0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Follow-up: Can you come up with an algorithm that is less than O(n2)
time complexity?
program main
implicit none
integer, parameter :: n = 4
integer, dimension(n) :: nums = [2, 7, 11, 15]
integer :: target = 9
integer, dimension(2) :: indices
call solve(nums, target, indices)
write (*, '(A, I0, A, I0)') 'Indices: ', indices(1), ', ', indices(2)
contains
subroutine solve(nums, target, indices)
implicit none
integer, dimension(:), intent(in) :: nums
integer, intent(in) :: target
integer, dimension(:), intent(out) :: indices
integer :: i, j
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) + nums(j) == target) then
indices(1) = i
indices(2) = j
return
end if
end do
end do
end subroutine solve
end program main
Indices: 1, 2
module solve_two_sum
implicit none
contains
function two_sum(nums, target) result(indices)
! 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:
! Input: nums = [2,7,11,15], target = 9
! Output: [0,1]
! Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
!
! Example 2:
! Input: nums = [3,2,4], target = 6
! Output: [1,2]
!
! Example 3:
! Input: nums = [3,3], target = 6
! Output: [0,1]
!
! 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?
integer, intent(in) :: nums(:), target
integer :: indices(2)
! Initialize the indices to -1 to indicate that they have not been found yet
indices = -1
! Loop through the array and find the first number that is equal to the target - nums(i)
do i = 1, size(nums)
if (nums(i) == target - nums(i)) then
! If the first number is found, set the indices to the current index and the previous index
indices = [i, i - 1]
exit
end if
end do
end function two_sum
end module solve_two_sum
program test_two_sum
use solve_two_sum, only: two_sum
implicit none
! Test case 1:
! Given nums = [2, 7, 11, 15], target = 9,
! return [0, 1].
integer, parameter :: nums1(4) = [2, 7, 11, 15]
integer, parameter :: target1 = 9
integer :: indices1(2)
! Test case 2:
! Given nums = [3, 2, 4], target = 6,
! return [1, 2].
integer, parameter :: nums2(3) = [3, 2, 4]
integer, parameter :: target2 = 6
integer :: indices2(2)
! Test case 3:
! Given nums = [3, 3], target = 6,
! return [0, 1].
integer, parameter :: nums3(2) = [3, 3]
integer, parameter :: target3 = 6
integer :: indices3(2)
! Test case 4:
! Given nums = [2, 7, 11, 15], target = 18,
! return [1, 2].
integer, parameter :: nums4(4) = [2, 7, 11, 15]
integer, parameter :: target4 = 18
integer :: indices4(2)
! Test case 5:
! Given nums = [2, 7, 11, 15], target = 100,
! return [0, 0].
integer, parameter :: nums5(4) = [2, 7, 11, 15]
integer, parameter :: target5 = 100
integer :: indices5(2)
! Test the function with the given examples
indices1 = two_sum(nums1, target1)
indices2 = two_sum(nums2, target2)
indices3 = two_sum(nums3, target3)
indices4 = two_sum(nums4, target4)
indices5 = two_sum(nums5, target5)
! Print the results
write (*,*) "Test case 1:", indices1
write (*,
temp.f95:38:4: 38 | do i = 1, size(nums) | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:51:5: 51 | use solve_two_sum, only: two_sum | 1 Fatal Error: Cannot open module file ‘solve_two_sum.mod’ for reading at (1): No such file or directory compilation terminated.
def twoSum(nums, target):
map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in map:
return [map[complement], i]
map[num] = i
return []
The algorithm leverages a hash map (unordered_map in C++, HashMap in Java, dictionary in Python, and Map in JavaScript). It iterates through the given 'nums' array and calculates the complementary value (target - current value). If the complementary value is already in the hash map, it means that we found a solution, and we return those indices. If the complement is not in the hash map, we store the current element in the hash map with its index. If the algorithm doesn't find the solution, it returns an empty array or throws an exception (in Java).
This approach has a time complexity of O(n) and a space complexity of O(n) as well.
#include <vector>
#include <unordered_map>
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (map.find(complement) != map.end()) {
return {map[complement], i};
}
map[nums[i]] = i;
}
return {};
}
The algorithm leverages a hash map (unordered_map in C++, HashMap in Java, dictionary in Python, and Map in JavaScript). It iterates through the given 'nums' array and calculates the complementary value (target - current value). If the complementary value is already in the hash map, it means that we found a solution, and we return those indices. If the complement is not in the hash map, we store the current element in the hash map with its index. If the algorithm doesn't find the solution, it returns an empty array or throws an exception (in Java).
This approach has a time complexity of O(n) and a space complexity of O(n) as well.