Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report for two-integer-sum-ii #3973

Open
dcamposliz opened this issue Mar 29, 2025 · 0 comments
Open

Bug Report for two-integer-sum-ii #3973

dcamposliz opened this issue Mar 29, 2025 · 0 comments

Comments

@dcamposliz
Copy link

dcamposliz commented Mar 29, 2025

Bug Report for https://neetcode.io/problems/two-integer-sum-ii

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Image

So the problem is asking to return the indices of the values that add up to the target. However, in the example, as listed in the problem shows the input and expected output:

Input: numbers = [1,2,3,4], target = 3

Output: [1,2]

The output should be [0, 1] assuming that we want the indices as the problem states.

I tried the solution below which works for one of the cases but not for the other one. Apologies if I am overlooking a glaring mistake here.

from typing import List

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        searching = True
        
        pointer_1 = 0
        pointer_2 = len(numbers) - 1
        
        while searching:
            print("Searching...")
            value_1 = numbers[pointer_1]
            value_2 = numbers[pointer_2]
            
            if value_1 + value_2 == target:
                return [pointer_1, pointer_2]
            
            if value_1 + value_2 > target:
                pointer_2 = pointer_2 - 1
            
            elif value_1 + value_2 < target:
                pointer_1 = pointer_1 + 1

Also, I noticed that one of the solutions listed in this problem increments the left and right pointer by 1. Not sure why this is:

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        l, r = 0, len(numbers) - 1

        while l < r:
            curSum = numbers[l] + numbers[r]

            if curSum > target:
                r -= 1
            elif curSum < target:
                l += 1
            else:
                return [l + 1, r + 1]
        return []
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

No branches or pull requests

1 participant