SHARE
TWEET

Untitled

a guest Jan 19th, 2013 542 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class SequenceOfMaxSum
  4. {
  5.     static void Main()
  6.     {
  7.         int[] array = { 23, 4, -4, -65, 3 };
  8.  
  9.         int max = array[0];
  10.         int maxEnd = array[0];
  11.         int longSequence = 1;
  12.         int currentSequence = 1;
  13.         int start = 0;
  14.         int startTemp = 0;
  15.  
  16.         for (int i = 1; i < array.Length; i++)
  17.         {
  18.             if (array[i] + maxEnd > array[i])
  19.             {
  20.                 maxEnd = array[i] + maxEnd;
  21.                 currentSequence++;
  22.             }
  23.  
  24.             else
  25.             {
  26.                 maxEnd = array[i];
  27.                 startTemp = i;
  28.                 currentSequence = 1;
  29.             }
  30.  
  31.             if (maxEnd > max)
  32.             {
  33.                 max = maxEnd;
  34.                 longSequence = currentSequence;
  35.                 start = startTemp;
  36.             }
  37.         }
  38.  
  39.         Console.WriteLine(max);
  40.  
  41.         for (int i = start; i < start + longSequence; i++)
  42.         {
  43.             Console.Write(array[i] + " ");
  44.         }
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top