Advertisement
Guest User

MaxSumOfElements

a guest
Dec 14th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.     class MaxSumOfSequnce
  8.     {
  9.         static void Main()
  10.         {
  11.             int[] array = { 2, 3, -6, -1, 2, -1, 6, 4, -8, 8 };
  12.             int searchSum = 0;
  13.             int findSum = 0;
  14.             int count = 0;
  15.             int startIndx = 0;
  16.             int endIndx = 0;
  17.  
  18.             while (count<array.Length)
  19.             {
  20.                 for (int i = count; i < array.Length; i++)
  21.                 {
  22.                         searchSum += array[i];
  23.                         if (searchSum > findSum)
  24.                         {
  25.                             findSum = searchSum;
  26.                             startIndx = count;
  27.                             endIndx = i;
  28.                         }
  29.                 }
  30.                 searchSum = 0;
  31.                 count++;
  32.             }
  33.  
  34.             Console.Write("Maximal sum is {0}.\nIndices of the elements are:\n", findSum);
  35.             for (int i = startIndx; i <=endIndx; i++)
  36.             {
  37.                 Console.WriteLine("[{0}]={1}", i, array[i]);
  38.             }
  39.  
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement