Need help with understanding how to approach sample question for Algorithmic Thinking.

Sample Question: http://ift.tt/1HJBok9

 

I am having trouble deciphering how to approach this question. I am pretty sure I know how to solve the actual algorithm but I have never used a data structure as a Return type before. I am also confused why I would Return a Tuple, when the Tuple is already created in the Main() method? I am not looking for any answers just a better understanding of how to proceed with the example.

 

If this were a List<{ 1, 3, 5, 7, 9 }> an Integer(12) I would use two nested loops or recursion to add the integers in List<> and see if the results match the integer.

 

/* * Write a function that, given a list and a target sum,
* returns zero-based indices of any two distinct elements whose sum is equal to the target sum.
* If there are no such elements, the function should return null.
* For example, FindTwoSum(new List<int>() { 1, 3, 5, 7, 9 }, 12) should return any of the following tuples of indices:
– 1, 4 (3 + 9 = 12),
– 2, 3 (5 + 7 = 12),
– 3, 2 (7 + 5 = 12) or
– 4, 1 (9 + 3 = 12).
*/

 

using System;
using System.Collections.Generic;

 

class TwoSum
{
public static Tuple<int, int> FindTwoSum(IList<int> list, int sum)
{
throw new NotImplementedException("Waiting to be implemented.");
}

public static void Main(string[] args) { Tuple<int, int> indices = FindTwoSum(new List<int>() { 1, 3, 5, 7, 9 }, 12); Console.WriteLine(indices.Item1 + " " + indices.Item2); } 

}

by xxsiriusxburnxx via /r/csharp

Leave a Reply