{"id":"6b093de0bd4755deb67ee0ad","slug":"two-sum","title":"Two items for the budget","summary":"An interview classic – find two different positions in a price list that add up to exactly the given amount. One pass with a map instead of two nested loops.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"easy","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["arrays","hashing","hash-map"],"languages":[{"lang":"typescript","runner":"browser","starter":"/**\n * Returns [i, j] with i < j and nums[i] + nums[j] === target\n * (smallest j first, then smallest i), or [] when there is no such pair.\n */\nfunction twoSum(nums: number[], target: number): number[] {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":10,"tests":"test('finds the basic pair', () => {\n  expect(twoSum([2, 7, 11, 15], 9)).toEqual([0, 1]);\n});\n\ntest('pair that is not adjacent', () => {\n  expect(twoSum([3, 2, 4], 6)).toEqual([1, 2]);\n});\n\ntest('two equal values at different positions', () => {\n  expect(twoSum([3, 3], 6)).toEqual([0, 1]);\n});\n\ntest('an element cannot pair with itself', () => {\n  expect(twoSum([5, 1], 10)).toEqual([]);\n  expect(twoSum([3], 6)).toEqual([]);\n});\n\ntest('negative numbers and zero', () => {\n  expect(twoSum([-4, 0, 9, 4, 13], 0)).toEqual([0, 3]);\n  expect(twoSum([-1, -2, -3, -4], -7)).toEqual([2, 3]);\n});\n\ntest('several pairs: the smallest j wins', () => {\n  expect(twoSum([1, 4, 2, 5], 6)).toEqual([1, 2]);\n});\n\ntest('duplicates: the smallest i for that j', () => {\n  expect(twoSum([2, 2, 5, 4], 6)).toEqual([0, 3]);\n});\n\ntest('empty input and no pair', () => {\n  expect(twoSum([], 1)).toEqual([]);\n  expect(twoSum([1, 2, 3], 100)).toEqual([]);\n});\n\ntest('large values', () => {\n  expect(twoSum([1000000000, -1000000000, 999999999], 1999999999)).toEqual([0, 2]);\n});\n\ntest('performance: 100 000 elements, pair at the very end', () => {\n  const n = 100000;\n  const nums = Array.from({ length: n }, (_, i) => 2 * i);\n  expect(twoSum(nums, nums[n - 2] + nums[n - 1])).toEqual([n - 2, n - 1]);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Returns [i, j] with i < j and nums[i] + nums[j] === target\n * (smallest j first, then smallest i), or [] when there is no such pair.\n */\nfunction twoSum(nums, target) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":10,"tests":"test('finds the basic pair', () => {\n  expect(twoSum([2, 7, 11, 15], 9)).toEqual([0, 1]);\n});\n\ntest('pair that is not adjacent', () => {\n  expect(twoSum([3, 2, 4], 6)).toEqual([1, 2]);\n});\n\ntest('two equal values at different positions', () => {\n  expect(twoSum([3, 3], 6)).toEqual([0, 1]);\n});\n\ntest('an element cannot pair with itself', () => {\n  expect(twoSum([5, 1], 10)).toEqual([]);\n  expect(twoSum([3], 6)).toEqual([]);\n});\n\ntest('negative numbers and zero', () => {\n  expect(twoSum([-4, 0, 9, 4, 13], 0)).toEqual([0, 3]);\n  expect(twoSum([-1, -2, -3, -4], -7)).toEqual([2, 3]);\n});\n\ntest('several pairs: the smallest j wins', () => {\n  expect(twoSum([1, 4, 2, 5], 6)).toEqual([1, 2]);\n});\n\ntest('duplicates: the smallest i for that j', () => {\n  expect(twoSum([2, 2, 5, 4], 6)).toEqual([0, 3]);\n});\n\ntest('empty input and no pair', () => {\n  expect(twoSum([], 1)).toEqual([]);\n  expect(twoSum([1, 2, 3], 100)).toEqual([]);\n});\n\ntest('large values', () => {\n  expect(twoSum([1000000000, -1000000000, 999999999], 1999999999)).toEqual([0, 2]);\n});\n\ntest('performance: 100 000 elements, pair at the very end', () => {\n  const n = 100000;\n  const nums = Array.from({ length: n }, (_, i) => 2 * i);\n  expect(twoSum(nums, nums[n - 2] + nums[n - 1])).toEqual([n - 2, n - 1]);\n});\n"},{"lang":"python","runner":"browser","starter":"def two_sum(nums: list[int], target: int) -> list[int]:\n    \"\"\"Returns [i, j] with i < j and nums[i] + nums[j] == target\n    (smallest j first, then smallest i), or [] when there is no such pair.\"\"\"\n    raise NotImplementedError\n","testCount":10,"tests":"def check(nums, target, expected):\n    got = two_sum(nums, target)\n    assert got == expected, f\"two_sum({nums[:8]}, {target}): expected {expected}, got {got}\"\n\n\ndef test_basic_pair():\n    check([2, 7, 11, 15], 9, [0, 1])\n\n\ndef test_pair_not_adjacent():\n    check([3, 2, 4], 6, [1, 2])\n\n\ndef test_two_equal_values():\n    check([3, 3], 6, [0, 1])\n\n\ndef test_element_cannot_pair_with_itself():\n    check([5, 1], 10, [])\n    check([3], 6, [])\n\n\ndef test_negative_numbers_and_zero():\n    check([-4, 0, 9, 4, 13], 0, [0, 3])\n    check([-1, -2, -3, -4], -7, [2, 3])\n\n\ndef test_smallest_j_wins():\n    check([1, 4, 2, 5], 6, [1, 2])\n\n\ndef test_duplicates_smallest_i():\n    check([2, 2, 5, 4], 6, [0, 3])\n\n\ndef test_empty_and_no_pair():\n    check([], 1, [])\n    check([1, 2, 3], 100, [])\n\n\ndef test_large_values():\n    check([1000000000, -1000000000, 999999999], 1999999999, [0, 2])\n\n\ndef test_performance_100k():\n    n = 100_000\n    nums = [2 * i for i in range(n)]\n    check(nums, nums[-2] + nums[-1], [n - 2, n - 1])\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\nusing System.Collections.Generic;\n\npublic static class Solution\n{\n    /// <summary>\n    /// Returns {i, j} with i &lt; j and nums[i] + nums[j] == target\n    /// (smallest j first, then smallest i), or an empty array when there is no such pair.\n    /// </summary>\n    public static int[] TwoSum(int[] nums, int target)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":10},{"lang":"java","runner":"judge0","starter":"import java.util.*;\n\nclass Solution {\n    /**\n     * Returns {i, j} with i < j and nums[i] + nums[j] == target\n     * (smallest j first, then smallest i), or an empty array when there is no such pair.\n     */\n    static int[] twoSum(int[] nums, int target) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":10},{"lang":"go","runner":"judge0","starter":"package main\n\n// TwoSum returns []int{i, j} with i < j and nums[i]+nums[j] == target\n// (smallest j first, then smallest i), or an empty slice when there is no such pair.\nfunc TwoSum(nums []int, target int) []int {\n\tpanic(\"not implemented\")\n}\n","testCount":10}],"questionCount":null,"xp":10,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:56.177Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A gift card is worth "},{"type":"text","text":"target","marks":[{"type":"code"}]},{"type":"text","text":" and has to be spent to the cent on "},{"type":"text","text":"two different","marks":[{"type":"bold"}]},{"type":"text","text":" products. Prices are in the array "},{"type":"text","text":"nums","marks":[{"type":"code"}]},{"type":"text","text":" (integers). Return the indices "},{"type":"text","text":"[i, j]","marks":[{"type":"code"}]},{"type":"text","text":" such that "},{"type":"text","text":"i < j","marks":[{"type":"code"}]},{"type":"text","text":" and "},{"type":"text","text":"nums[i] + nums[j] == target","marks":[{"type":"code"}]},{"type":"text","text":"."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"When several pairs match, the one with the "},{"type":"text","text":"smallest ","marks":[{"type":"bold"}]},{"type":"text","text":"j","marks":[{"type":"code"}]},{"type":"text","text":" wins; for the same "},{"type":"text","text":"j","marks":[{"type":"code"}]},{"type":"text","text":", the smallest "},{"type":"text","text":"i","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"An element cannot be used twice (two equal prices at different positions are fine)."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"When there is no pair, return an empty array."}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Signatures: "},{"type":"text","text":"twoSum(nums, target)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"two_sum(nums, target)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.TwoSum","marks":[{"type":"code"}]},{"type":"text","text":" (C#), "},{"type":"text","text":"Solution.twoSum","marks":[{"type":"code"}]},{"type":"text","text":" (Java), "},{"type":"text","text":"TwoSum","marks":[{"type":"code"}]},{"type":"text","text":" (Go)."}]},{"type":"heading","attrs":{"level":2,"id":"examples"},"content":[{"type":"text","text":"Examples"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"nums","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"target","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Result"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Why"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[2, 7, 11, 15]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"9","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, 1]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2 + 7"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[1, 4, 2, 5]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"6","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[1, 2]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"pairs (0, 3) and (1, 2) – (1, 2) has the smaller "},{"type":"text","text":"j","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[2, 2, 5, 4]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"6","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, 3]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"for "},{"type":"text","text":"j = 3","marks":[{"type":"code"}]},{"type":"text","text":" the smallest "},{"type":"text","text":"i","marks":[{"type":"code"}]},{"type":"text","text":" is 0"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[5, 1]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"10","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"5 cannot be used twice"}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"0 ≤ nums.length ≤ 100,000","marks":[{"type":"code"}]},{"type":"text","text":"; values and "},{"type":"text","text":"target","marks":[{"type":"code"}]},{"type":"text","text":" fit in a 32-bit "},{"type":"text","text":"int","marks":[{"type":"code"}]},{"type":"text","text":", but the sum of two values may not (in C# and Java compute in "},{"type":"text","text":"long","marks":[{"type":"code"}]},{"type":"text","text":")."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Values may repeat."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"O(n)","marks":[{"type":"bold"}]},{"type":"text","text":" time, "},{"type":"text","text":"O(n)","marks":[{"type":"bold"}]},{"type":"text","text":" extra memory. An O(n²) solution will not fit in the time limit of the performance test."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/two-sum/","title":"LeetCode 1 – Two Sum","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:56.178Z","updatedAt":"2026-11-27T08:22:56.178Z"}