title: get maximum sum of consecutive elements in an array
body: <p>I want to get the maximum sum of three consecutive elements in an array and the index of the starting element of the maximum sum.</p>
  
  <p>In a given array,</p>
  
  <pre><code>int a[] = {2,1,3,2,4,6,7,9,1}     
  </code></pre>
  
  <p>maximum <code>sum = 6 + 7 + 9 = 22</code>
  starting index of maximum sum is <code>5</code> (index of the starting item -<code>6</code>). </p>
  
  <p>I have written the code as below but it's not performance efficient. Not sure if using <em>Linq</em> gives better results</p>
  
  <p>Could anyone please suggest any better approach.</p>
  
  <pre><code>private static int[] getConsecutiveSum(int[] a, int k)
          {
              List&lt;int&gt; lst = new List&lt;int&gt;();
              for (int x = 0; x &lt; a.Length; x++)
              {
                  lst.Add(getsum(a, x, k));
              }
  
              var max = lst[0];
              var index = 0;
              for(var i = 0; i &lt;lst.Count; i++)
              {
                  if(lst[i] &gt; max)
                  {
                      max = lst[i];
                      index = i;
                  }
              }
              int[] result = new int[2];
              result[0] = max;
              result[1] = index;
              return result;
          }
  
          private static int getsum(int[] a, int s, int e)
          {
              var sum = 0;
              var loop = 1;
              for (int x = s; x &lt; a.Length; x++)
              {
                  sum = sum + a[x];
                  loop++;
                  if (loop &gt; e) break;
              }
              if (loop &gt; e) return sum;
              else return 0;
          }
  }
  </code></pre>