Skip to content

All Pairs

Get access to track progress Get access to bookmark
Asset Pricingx / 34
Behavioral Questionsx / 7
Brain Teasersx / 77
Derivatives Theoryx / 107
Digital Assets - Cryptox / 82
Energy Tradingx / 55
Linear Algebrax / 24
Machine Learningx / 62
Math Questionsx / 49
Probability and Statisticsx / 202
Programmingx / 54
Totalx / 753
Write a function that takes in an array of integers, which can be both positive and negative, along with a target integer. The function should return all unique pairs of integers from the array that sum up to the target integer.
def find_pairs_with_sum(arr: List[int], target: int) -> List[Tuple[int, int]]:

pass

Inputs
The inputs are defined as follows:
  • arr: A list of integers containing both positive and negative numbers. The array may have duplicate values.
  • target: An integer that we are interested in finding pairs for.
Output
Return a list of tuples, where each tuple contains a pair of integers that sum up to the target integer.

Example Tests
  • find_pairs_with_sum([3, 4, 2, 5], 7) should return [(3, 4), (2, 5)]
  • find_pairs_with_sum([2, 4, 1, 5], 9) should return [(4, 5)]
  • find_pairs_with_sum([7, 1, 2, 3, 4, -2], 5) should return [(1, 4), (2, 3), (7, -2)]
from typing import List, Tuple, Set


def find_pairs_with_sum(arr: List[int], target: int) -> List[Tuple[int, int]]:
# Initialize an empty set to store unique pairs
result_set = set()

# Create a set for quick lookups
complementary_set = set()

# Iterate through the array to find pairs that sum up to the target
for num in arr:
complement = target - num
if complement in complementary_set:
# Sort the pair (smaller, larger) before adding to result set for uniqueness
result_set.add(tuple(sorted((num, complement))))
complementary_set.add(num)

# Convert the set to a list of tuples
return list(result_set)

# Test the function with the given test cases
test_cases = [
([3, 4, 2, 5], 7),
([2, 4, 1, 5], 9),
([7, 1, 2, 3, 4, -2], 5)
]

for arr, target in test_cases:
print(f"find_pairs_with_sum({arr}, {target}) = {find_pairs_with_sum(arr, target)}")
Python scratchpad
# Run Python right here in your browser
def two_sum(nums, target):
    seen = {}
    for i, n in enumerate(nums):
        if target - n in seen:
            return [seen[target - n], i]
        seen[n] = i
    return []

print(two_sum([2, 7, 11, 15], 9))  # [0, 1]
Run Python in your browser

Write and execute code — numpy, pandas & scikit-learn included — right on the question page.

Log in to start coding
Title Category Subcategory Difficulty Status
All Permutations ProgrammingArray and List ProcessingMedium
Arithmetic Mean ProgrammingArray and List ProcessingHard
Binary Search Tree (BST) ProgrammingArray and List ProcessingHard
Counting Inversions ProgrammingArray and List ProcessingHard
Crate Loading ProgrammingArray and List ProcessingEasy
Find the Missing Number ProgrammingArray and List ProcessingEasy
Finding the Largest Possible Number ProgrammingArray and List ProcessingEasy
Longest Common Subsequence (LCS) ProgrammingArray and List ProcessingMedium
Example
Longest Increasing Subsequence ProgrammingArray and List ProcessingHard
Majority Element ProgrammingArray and List ProcessingMedium
Maximum Subarray Sum ProgrammingArray and List ProcessingMedium
Search a Rotated Array ProgrammingArray and List ProcessingMedium
Settling Array ProgrammingArray and List ProcessingMedium
Sorting Without Sort() ProgrammingArray and List ProcessingHard
Subarray Sum Equals Zero ProgrammingArray and List ProcessingMedium
Sudoku ProgrammingArray and List ProcessingHard

Please log in to see the discussion.