Summing 3 consecutive integers in a list to see if the sum matches a specified value I have a program where users can add integers to a list and then I want to check the list to see if any three consecutive integers in the list sum to a specified value. I have been told that it is taking too long when ran with a large number of integers in the list. The timing test that I am not passing is based upon looking for the first match of the sum of 3 consecutive integers, in a large list of integers, to one specific value. The specific value can vary with each run but not within the run and the single run is that is being timed.

class Aggro
{    
    private List&lt;int&gt; _numbers = new List&lt;int&gt;();
    public void AddToList(int[] list)
    {
        for (int i = 0; i &lt; list.Length; i++ )
        {
            _numbers.Add(list[i]);
        }
    }

    public bool SumExists(int sum)
    {
        int[] numbers = _numbers.ToArray();
        for (int i = 0; i &lt; numbers.Length -2; i++)
       {
            if (numbers[i] + numbers[i + 1] + numbers[i + 2] == sum)
            {
                return true;
            }
        }
        return false;
    }

    public static void Main(string[] args)
    {
        Aggro a = new Aggro();
        a.AddToList(new int[] { 12, 21, 16, 4, 67, 1 });
        Console.WriteLine(a.SumExists(41)); // True
        Console.WriteLine(s.SumExists(69)); // False
        a.AddToList(new int[] { 1 });
        Console.WriteLine(a.ContainsSum3(69)); //True
    }
}
About performance:

One call of a.AddToList plus one call of a.SumExists cannot work conceptually faster than your variant. But several calls of a.AddToList plus several calls of a.SumExists can run much faster.


You can index all the sums after first call to a.SumExists with as HashSet, so all other calls of SumExists will be run in \$O(1)\$.
A second (and others) call to a.AddToList needs no recalc the whole index - you need only add new elements.




class Aggro
{    
    List&lt;int&gt; _numbers = new List&lt;int&gt;();
    HashSet&lt;int&gt; _index = new HashSet&lt;int&gt;();
    int _lastIndexed = -1;

    public void AddToList(int[] list)
    {
        for (int i = 0; i &lt; list.Length; i++ )
        {
            _numbers.Add(list[i]);
        }
    }

    public bool SumExists(int sum)
    {
       if (_lastIndexed &gt; -1 &amp;&amp; _index.Contains(sum)) return true;
       for (int i = _lastIndexed + 1, c = numbers.Length - 2; i &lt; c; i++)
       {
            int tmp = numbers[i] + numbers[i + 1] + numbers[i + 2];
            _index.Add(tmp);
            _lastIndexed = i;
            if (tmp == sum)  return true;
        }
        return false;
    }

    public static void Main(string[] args)
    {
        Aggro a = new Aggro();
        a.AddToList(new int[] { 12, 21, 16, 4, 67, 1 });
        Console.WriteLine(a.SumExists(41)); // True
        Console.WriteLine(s.SumExists(69)); // False
        a.AddToList(new int[] { 1 });
        Console.WriteLine(a.ContainsSum3(69)); //True
    }
}