FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

Ā© 2026 FlareStart. All rights reserved.

Back to articles
Two Sum Problem – Python Solution
NewsProgramming Languages

Two Sum Problem – Python Solution

via Dev.to PythonManoj Kumar3h ago

🧠 Two Sum Problem – Python Solution Hi All, Today I solved a popular problem from LeetCode called Two Sum . šŸ“Œ Problem Statement Given an array of integers nums and an integer target , return the indices of two numbers such that they add up to the target. Conditions: Each input has exactly one solution You cannot use the same element twice You can return the answer in any order šŸ’” Approach šŸ”¹ Brute Force (Not efficient) Check every pair using two loops Time Complexity: O(n²) šŸ”¹ Optimized Approach (Used) Use a dictionary (hash map) Store number and its index For each element: Find target - current number Check if it exists in dictionary šŸ‘‰ This reduces time complexity to O(n) šŸ’» Python Code class Solution : def twoSum ( self , nums , target ): num_map = {} for i in range ( len ( nums )): complement = target - nums [ i ] if complement in num_map : return [ num_map [ complement ], i ] num_map [ nums [ i ]] = i šŸ” Example Walkthrough Input: nums = [ 2 , 7 , 11 , 15 ] target = 9 Steps: i = 0 → num

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
0 views

Related Articles

News

My home network observes bedtime with OpenBSD and pf

Lobsters • 16m ago

What Is URL Encoding and Why Does Your Link Look Like %20%3F%26
News

What Is URL Encoding and Why Does Your Link Look Like %20%3F%26

Medium Programming • 37m ago

The secret story of the vocoder, the military tech that changed music forever
News

The secret story of the vocoder, the military tech that changed music forever

The Verge • 42m ago

Programming Is Not Just About Syntax
News

Programming Is Not Just About Syntax

Medium Programming • 44m ago

How I Actually Start Low-Level Design Before Thinking About Design Patterns
News

How I Actually Start Low-Level Design Before Thinking About Design Patterns

Medium Programming • 48m ago

Discover More Articles