Two Sums

The Two Sum question is a leetcode question that works on finding two numbers in an array whose sum equals a given target Let’s understand the question: Given an array nums and a target value, retu...

By · · 2 min read
Two Sums

Source: DEV Community

The Two Sum question is a leetcode question that works on finding two numbers in an array whose sum equals a given target Let’s understand the question: Given an array nums and a target value, return the indices of the two numbers such that they add up to the target. example target = 9 Output : [2,7] because 2+ 7 -> 9 *Approach * I came to know there are 3 ways to solve this question they were *brute force - > comparing all possible pairs hashing -> store all previous traverse optimized -> by difference * I choose optimized approach it is the most time and memory efficent approach, I started with a question "Do I really need to check all pairs?” That felt slow and so was hashing “Can I remember what I’ve already seen?” step 1: Target - current number and move that value to another list dif = [] loop 1 nums = [2, 7, 11, 15] dif = [] 9 - nums[0] - > 2 2 not in dif so -> dif = [2] loop 2 nums = [2, 7, 11, 15] 9-nums[1] -> 2 2 in dif then that means the pair is found A

Related Posts

Similar Topics

#data science (732)#machine learning (294)#programming (241)#ai (170)#hands on tutorials (159)#editors pick (154)#artificial intelligence (118)#data visualization (93)#deep dives (144)#for (113)#learn (108)#deep learning (74)#tutorial (83)#statistics (80)#pandas (81)#llm (68)#data analysis (66)#webdev (62)#geospatial (59)#tips and tricks (57)

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#artificial intelligence (31582)#data science (24018)#ai (17263)#generative ai (15034)#crypto (15016)#machine learning (14681)#bitcoin (14270)#featured (13571)#news & insights (13064)#crypto news (11094)

Around the Network