Two Sum — Find Pair with Given Sum

A simple problem that explains the Two Sum problem in Python with clear logic, code, flowchart steps, pseudocode, time complexity, space complexity, and test case

01. Introduction

Python basic programs for beginners should teach real thinking, not just syntax. The Two Sum problem is a classic example because it helps you learn how to search for numbers, use a dictionary, and solve a problem in a smart way. In this blog, you will understand the problem in simple English and learn how the two-sum problem in Python program works step by step.

02. Problem Statement

Given a list of integers and a target sum, find two numbers in the list whose sum is equal to the target. Return the indices of those two numbers.

03. Input Format

Input 1

A list of integers called nums.

Input 2

An integer called target.

Example: nums = [2, 7, 11, 15], target = 9

04. Output Format

Return the indices of the two numbers that add up to the target.

Example Output: [0, 1]

05. Flowchart

 Two Sum problem in python flowchart

 

06. Algorithm

  1. Read the list of numbers (nums) and target value (target)
  2. Create an empty dictionary prev_map
  3. Loop through each element in the list using index i
  4. For each number n, calculate:
    diff = target - n
  5. Check if diff exists in prev_map
    • If yes → return indices [prev_map[diff], i]
  6. If not → store current number in dictionary:
    prev_map[n] = i
  7. Continue the loop until a pair is found
  8. If no pair exists → return an empty list []

07. Pseudocode

INPUT nums (list of integers)
INPUT target (integer)
CREATE empty dictionary prev_map
FOR each index i from 0 to length of nums – 1 DO
SET n = nums[i]
SET diff = target – n
IF diff exists in prev_map THEN
RETURN [prev_map[diff], i]
END IF
STORE n in prev_map with index i
END FOR
RETURN empty list

08. Python Program

nums = list(map(int, input().split()))
target = int(input())
def two_sum(nums, target):
    prev_map = {}
    for i, n in enumerate(nums):
        diff = target – n
        if diff in prev_map:
            return [prev_map[diff], i]
        prev_map[n] = i
    return []
print(two_sum(nums, target))

09. Explanation 

Line-1

nums = list(map(int, input().split()))

What it does:

  • Takes input from user (like: 2 7 11 15)
  • input().split() → splits into strings → ["2", "7", "11", "15"]
  • map(int, ...) → converts each to integer
  • list(...) → makes it a list

Line 2

target = int(input())

What it does:

  • Takes another input (like 9)
  • Converts it into integer

 Line-3

def two_sum(nums, target):

What it does:

  • Defines a function named two_sum
  • It takes:
    • nums → list of numbers
    • target → required sum

 Line 4

prev_map = {}

What it does:

  • Creates an empty dictionary

 Line 5

for i, n in enumerate(nums):

What it does:

  • Loops through list
  • enumerate() gives:
    • i → index
    • n → value

 Line 6

diff = target n

What it does:

  • Finds the number needed to reach target

 Line 7–8

if diff in prev_map:
return [prev_map[diff], i]

What it does:

  • Checks: “Have we already seen the required number?”

Line 9

prev_map[n] = i

What it does:

  • Stores current number and index

 Line 10

return []

What it does:

  • If no pair is found → return empty list

 Last Line

print(two_sum(nums, target))

What it does:

  • Calls the function
  • Prints the result

10. Time Complexity

O(n) because the program goes through the list only one time.

Space Complexity

O(n) because the dictionary may store all numbers in the list.

11. Test Cases

Example 1

Input / Output

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: 2 + 7 = 9

Example 2

Input / Output

Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Explanation: 2 + 4 = 6

12. Conclusion

The Two Sum problem is a great beginner problem because it teaches fast thinking, dictionary use, and clean logic. It is one of the best python basic programs for beginners to build strong coding habits. Once you understand this problem, many other programming tasks become easier.

 

Leave a Comment