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.
04. Output Format
Return the indices of the two numbers that add up to the target.
05. Flowchart
06. Algorithm
- Read the list of numbers (
nums) and target value (target) - Create an empty dictionary
prev_map - Loop through each element in the list using index
i - For each number
n, calculate:
→diff = target - n - Check if
diffexists inprev_map- If yes → return indices
[prev_map[diff], i]
- If yes → return indices
- If not → store current number in dictionary:
→prev_map[n] = i - Continue the loop until a pair is found
- 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
09. Explanation
Line-1
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 integerlist(...)→ makes it a list
Line 2
What it does:
- Takes another input (like
9) - Converts it into integer
Line-3
What it does:
- Defines a function named
two_sum - It takes:
nums→ list of numberstarget→ required sum
Line 4
What it does:
- Creates an empty dictionary
Line 5
What it does:
- Loops through list
enumerate()gives:i→ indexn→ value
Line 6
What it does:
- Finds the number needed to reach target
Line 7–8
return [prev_map[diff], i]
What it does:
- Checks: “Have we already seen the required number?”
Line 9
What it does:
- Stores current number and index
Line 10
What it does:
- If no pair is found → return empty list
Last Line
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.
