Skip to content

Instantly share code, notes, and snippets.

@JL-Cox
Last active July 3, 2018 23:18
Show Gist options
  • Save JL-Cox/a174a548c26a0ee4233ba13800525db2 to your computer and use it in GitHub Desktop.
Save JL-Cox/a174a548c26a0ee4233ba13800525db2 to your computer and use it in GitHub Desktop.
CodeWarsKata: TwoSum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Verified Correct Answer
namespace CodeWarsKata_TwoSum_06252018_
{
class Program
{
//Test Variables for TwoSum Method
static void Main(string[] args)
{
int[] numbers = { 2, 2, 3 };
int target = 4;
Console.WriteLine(TwoSum(numbers, target));
}
public static int[] TwoSum(int[] numbers, int target)
{
//We know that the answer is going to be an array of two indexOf values
int[] answer = {0,0};
//The variable we use to test the True/False of the sums of each variable in the "numbers" array
int sum = 0;
//Interlocked "for" loops make for an easy mechanism to test any give two variables in an array against one another
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < numbers.Length; j++)
{
if (j == i) j++;
sum = numbers[i] + numbers[j];
//If this is ever true, then you know you have your answer. Return result.
if (sum == target)
{
answer[0] = j;
answer[1] = i;
return answer;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment