Skip to content

Conversation

@ViswaNikhitha
Copy link

Description

This pull request adds an efficient Python solution for the classic Two Sum problem.

The implementation uses a hash map (dictionary) to store previously seen values and their indices, allowing the solution to run in linear time.

Changes Made

  • Implemented twoSum method inside a Solution class
  • Used dictionary-based lookup to achieve O(n) time complexity
  • Ensured compatibility with Python 2 and Python 3 (no type hints)

Approach

  • Iterate through the array once
  • For each element, compute the required complement (target - current value)
  • Check if the complement exists in the dictionary
  • Return indices once the pair is found

Time & Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Testing

Test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant