Two Sum Leetcode – Getting [0,0] on multiple lists input

Question:

I’m a beginner and trying to solve the two sum leetcode (easy problem).
I know my code is kind of basic but it works when I try it in another workspace(codecademy) and it also works if the leet code input is only 1 list.

However, when leetcode applies 2-3 lists(testcases on the site), the 2nd and 3rd list returns [0,0]. Anyone knows why it returns [0,0]?.
I think it’s related to line 2 of the code, which was provided by leetcode at the start, related to Class solution: def twoSum(self, nums: List[int], target: int) -> List[int]: because when I provide a different list in Codecademy, it works.

I also tried removing break since I thought that stops it from getting multiple inputs but it doesn’t fix the issue.

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        lengthlist = len(nums)       
        a = 0
        b = 0
        
        for numbera in nums:
            for numberb in nums:
                if nums[a] + nums[b] == target:
                    if a < b:
                        return [a,b]
                        break
                    else:
                        return [b,a]
                        break
                else:
                    continue
                a += 1
            b += 1

EDIT / CONCLUSION:
Found the issue with the code based from the comments.

The leetcode problem specifies that I may not use the same element twice.
I did not account for this since I’m using range(0, len(nums)) on both for loops. I realized my code only worked if I used i+1 when I copied AboAmar’s reply at the start of the 2nd for loop.

The 2nd test case has [3,2,4] with 6 target. My code was returning [0,0] because 3+3 is 6. But leetcode outputs [1,2] because I can’t use the same element twice in the list.

Asked By: Lance

||

Answers:

A straightforward loop, since the order is not important and only one solution is assumed.

def twoSum(self, nums, target):
    for i in range(len(nums)):
        for j in range(i+1,len(nums)):
            if nums[i] + nums[j] == target:
                return [i, j]

As you can see, you need both the index and the value. The index of the inner loop j takes from the next i to the end of the array. So, you don’t need the comparisons a < b, etc. Also, break is not needed as you already return upon finding the solution.

Answered By: AboAmmar
class Solution {
public int[] twoSum(int[] nums, int target) {
    int first=-1,second=-1;
    boolean flag=false;

    for(int i=0; i<nums.length; i++)
    {
        for(int j=0; j<nums.length; j++)
        {
            if(i!=j)
            {
                if(nums[i]+nums[j]==target)
                {
                    first=i;
                    second=j;
                    flag=true;
                }
            }
        }
        if(flag==true)
            break;
    }
    int[] arr= new int[2];
    arr[0]=first;
    arr[1]=second;

    return(arr);
}
}
Answered By: Ali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.