Two Sum II - Input Array Is Sorted

In this task, I worked on finding two numbers in a sorted array that add up to a given target value. This problem helped me understand how to efficiently use pointers instead of checking all possib...

By · · 1 min read
Two Sum II - Input Array Is Sorted

Source: DEV Community

In this task, I worked on finding two numbers in a sorted array that add up to a given target value. This problem helped me understand how to efficiently use pointers instead of checking all possible pairs. What I Did I created a function twoSum that takes a sorted list of numbers and a target value. The function returns the positions of the two numbers whose sum equals the target. For example: Input: numbers = [2, 7, 11, 15], target = 9 Output: [1, 2] This means the numbers at positions 1 and 2 add up to 9. How I Solved It To solve this, I used two pointers: l starting from the beginning of the array r starting from the end of the array Then I checked the sum of the elements at these positions. At each step: If the sum equals the target, I return their positions If the sum is less than the target, I move the left pointer forward If the sum is greater than the target, I move the right pointer backward Code class Solution: def twoSum(self, numbers: list[int], target: int) -> list[int

Related Posts

Similar Topics

#data science (350)#programming (253)#machine learning (249)#artificial intelligence (125)#for (113)#learn (107)#ai (91)#tutorial (57)#python (64)#optimization (45)#editors pick (57)#hands on tutorials (39)#webdev (49)#deep dives (42)#productivity (40)#math (33)#mathematics (26)#instrument (15)#software engineering (17)#chatgpt (13)

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 (31585)#data science (24018)#ai (17359)#generative ai (15034)#crypto (15025)#machine learning (14681)#bitcoin (14281)#featured (13571)#news & insights (13064)#crypto news (11103)

Around the Network