DEV Community

JAYA SRI J
JAYA SRI J

Posted on

Two sum

The Two Sum problem is one of the most important problems in programming interviews. It tests how efficiently you can:
Search for values
Use data structures
Optimize time complexity

Instead of checking all pairs (which is slow), we use a dictionary (hash map) to solve it efficiently.

Problem Statement
You are given:

A list of integers → nums
A target integer → target

Goal:
Find two indices such that:

nums[i] + nums[j] = target

Return those indices.

my code:


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        d = {}
        for i in range(0, len(nums)):
            value = nums[i]
            difference = target - value
            if value not in d:
                d[difference] = i
            else:
                current_index = i
                prev_index = d[value]
                return [current_index, prev_index]
Enter fullscreen mode Exit fullscreen mode

Understanding the Code idea

This approach works differently from the standard one.

Standard Method:
Store → number : index
Your Method:
Store → required number (difference) : index
Meaning:
Instead of storing what we have seen,
we store what we are expecting to see later.

Step-by-Step Line Explanation

  1. Class Definition
    class Solution:
    Why?
    Required for platforms like LeetCode
    Organizes the solution

  2. Function Definition
    def twoSum(self, nums: List[int], target: int) -> List[int]:
    Why?
    nums → input list
    target → desired sum
    Returns list of indices

  3. Create Dictionary
    d = {}
    Why?
    This dictionary stores future expectations
    Structure:

d = {
    required_value : index
}
Enter fullscreen mode Exit fullscreen mode
  1. Loop Through List
for i in range(0, len(nums)):
Enter fullscreen mode Exit fullscreen mode

Why?
Traverse each element
Access index using i

  1. Get Current Value
value = nums[i]
Enter fullscreen mode Exit fullscreen mode

Why?
Extract the current number from list

  1. Calculate Difference difference = target - value Why?

We want:

value + difference = target

So:

difference = target - value

  1. Check Condition
    if value not in d:
    Why?
    Check if current value was expected earlier
    Meaning:
    “Did we already store this number as a required value?”

  2. Store Difference
    d[difference] = i
    Why?
    We store what we need in the future
    Example:
    target = 9
    value = 2
    difference = 7

Store → {7: 0}

Meaning:
“If I see 7 later, I can form the answer.”

  1. Else Condition (Match Found)
    else:
    Why?
    If value is already in dictionary
    That means we found the required pair

  2. Store Indices

current_index = i
prev_index = d[value]
Enter fullscreen mode Exit fullscreen mode

Why?
current_index → current element
prev_index → index where this value was expected

  1. Return Answer

return [current_index, prev_index]
Enter fullscreen mode Exit fullscreen mode

Why?
Return indices of the two numbers

Top comments (0)