title: Understanding why my code for the Two Sum (LeetCode problem) in C# does not work for expected answer of [1,2]
body: <p>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.</p>
  <p>For example, you have nums = [2,7,11,15] and target= 9. The expected output would be [0,1] because 2+7 = 9.</p>
  <p>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]</p>
  <p>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.</p>
  <p>Code that I used for this problem:</p>
  <pre><code>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&lt;= nums.Length-1 ; i++) 
          {
              
  
              if(i&lt; 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 &lt; int &gt; ();
              }           
  
          }
          return new int[0]; //Returns an empty array if the solution is not found
      }
  }
  </code></pre>
  <p>Thank you in advance!</p>