Home > Enterprise >  Understanding why my code for the Two Sum (LeetCode problem) in C# does not work for expected answer
Understanding why my code for the Two Sum (LeetCode problem) in C# does not work for expected answer

Time:12-23

I'm trying to solve the Two Sum LeetCode problem. In this problem, you are given an array of integers named nums and a variable containing an integer named target. The problem is asking to return an array that contains the indices of the two numbers that add up to the target.

For example, you have nums = [2,7,11,15] and target= 9. The expected output would be [0,1] because 2 7 = 9.

My code (which is shown below) works for the above example, however, it doesn't work for the second example that LeetCode checks: nums= [3,2,4], target = [6], expected outcome is [1,2]

I would like to know why my code isn't working for the second example and how I can modify it so it can work.

Code that I used for this problem:

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        //Initializing i, k and sum variables
        int i=0;
        int k= 0;
        int sum= 0;
        
        for(i=0; i<= nums.Length-1 ; i  ) 
        {
            

            if(i< nums.Length)
            {
                k= i 1;
            
                sum= nums[i]   nums[k];
                
                if(sum== target)
                {
                    //Returns an array in the compiler containing the values of i and k.
                    return new int[] {i, k};
                } else 
                {
                    return new int[0];
                }
                
                
            } else
            {
                return Array.Empty < int > ();
            }           

        }
        return new int[0]; //Returns an empty array if the solution is not found
    }
}

Thank you in advance!

CodePudding user response:

Your issue is the else block here

        if(sum== target)
        {
            //Returns an array in the compiler containing the values of i and k.
            return new int[] {i, k};
        } else 
        {
            return new int[0];
        }

When you don't hit the target with indexes 0 and 1, you return the empty array instead of trying with indexes 1 and 2.

If you delete the else block, your code will work with the example you've given.

CodePudding user response:

Your problem is the else block which has a return so your loop will not work.

But you can just use:


// bit more array items to test
var nums = new[] { 2, 7, 11, 0, 15, 2, 9 };
int target = 9;

// Your second test case
// var nums = new[] { 3, 2, 4 };
// int target = 6;

var results = new Dictionary<int, int>();
for (var i = 0; i < nums.Length; i  )
{
    var currI = nums[i];
    for (var x = 0; x < nums.Length; x  )
    {
        var currX = nums[x];
        var sum = currI   currX;

        // just with the correct sum and it is not already in the result
        if (sum == target && i != x && !results.ContainsKey(x))
        {
            results[i] = x;
        }
    }
}

This will result in

0, 1
1, 5
3, 6

If you just need to know the first combination do a .FirstOrDefault()

Working full example can be found here: dotnetfiddle

CodePudding user response:

you can semplify your code:

using System;

public class Program
{
    public static void Main()
    {
        var nums = new[] { 2, 7, 11, 0, 15, 2, 9 };
        int target = 9;

        for (var i = 0; i < nums.Length; i  )
            for (var x = i; x < nums.Length; x  )
                if (nums[i]   nums[x] == target)
                    Console.WriteLine($"{i}, {x}");

    }
}
  •  Tags:  
  • c#
  • Related