You are given an array of n integers and a number k. Determine whether there is a pair of elements in the array that sums to exactly k. For example, given the array [1, 3, 7] and k = 8, the answer is “yes,” but given k = 6 the answer is “no.”


Solving Two Sum with a brute force method is very simple, but not the quickest of all solutions. We loop through each element x and find if there is another value that equals to target - x

Two Sum Brute Force Gif

func twoSumSolutionBruteForce(_ nums: [Int], _ target: Int) -> [Int] {
for i in 0..<nums.count {
for j in (i + 1)..<nums.count {
if nums[j] == target - nums[i] {
return [i, j]
return [0, 0]


Stanford - Interview Question: The Two­Sum Problem

Leetcode - Two Sum