Pair Finder: Sum to Target ๐ŸŒฑ
README.md
main.py
notes.txt
Output
Tests

Pair Finder: Sum to Target

๐ŸŒฑ Beginner Project 2558635468

๐Ÿฆ„ Pair Finder: Sum to Target

Difficulty: Beginner Tags: arrays, hashmap, two-pointer, complement-pattern Series: CS 101


Problem

In the mystical marketplace, you're searching for two magical items whose combined power equals a specific target. Can you find if such a pair exists?

Given an array of integers nums and an integer target, return True if any two numbers in the array sum to the target, otherwise return False.

Real-World Application

The two-sum pattern appears in: - Financial transactions - finding transactions that balance out - Inventory management - pairing items to meet order requirements - Chemistry - finding compounds that combine to target molecular weight - Resource allocation - matching resources to meet capacity needs - Cryptography - subset sum problems in security analysis - Game development - combining items to achieve target stats - Network optimization - pairing nodes for load balancing

Input

data = {
    'nums': list[int],  # Array of integers
    'target': int       # Target sum
}

Output

bool  # True if pair exists, False otherwise

Constraints

  • Array length โ‰ค 100,000
  • Integers can be negative, zero, or positive
  • Need to find exactly two numbers (not one number used twice)

Examples

Example 1: Pair Found

Input: {'nums': [2, 7, 11, 15], 'target': 9}

Output: True

Explanation: - 2 + 7 = 9 โœ“ - Pair exists at indices 0 and 1

Example 2: No Pair Found

Input: {'nums': [1, 2, 3], 'target': 7}

Output: False

Explanation: - No two numbers sum to 7 - Maximum possible sum: 2 + 3 = 5

Example 3: Negative Numbers

Input: {'nums': [-3, 4, 3, 90], 'target': 0}

Output: True

Explanation: - -3 + 3 = 0 โœ“ - Works with negative numbers

Example 4: Duplicate Values

Input: {'nums': [3, 3], 'target': 6}

Output: True

Explanation: - 3 + 3 = 6 โœ“ - Two different 3's can pair

Example 5: Single Element

Input: {'nums': [5], 'target': 10}

Output: False

Explanation: - Need two numbers, only have one - Cannot use same element twice

Example 6: Target is Zero

Input: {'nums': [-1, 0, 1, 2], 'target': 0}

Output: True

Explanation: - -1 + 1 = 0 โœ“ - Multiple valid pairs exist


What You'll Learn

  • The complement pattern (target - current = needed)
  • Using hash sets for O(1) lookups
  • Single-pass algorithms with early exit
  • The most common interview question pattern

Why This Matters

This is the #1 most asked coding interview question. It introduces: - Hash table usage for optimization - The complement search pattern - Trade-offs between time and space complexity - Foundation for more complex sum problems (3-sum, 4-sum)



Starter Code

def challenge_function(data):
    """
    Check if any two numbers in array sum to target.

    Args:
        data: dict with keys:
            - 'nums' (list[int]): array of integers
            - 'target' (int): target sum

    Returns:
        bool: True if pair exists, False otherwise
    """
    # Your implementation here
    pass
OUTPUT
TESTS
Output
Code execution results displayed hereโ€ฆ
Test Results
Test results displayed hereโ€ฆ