Advertisement
stanevplamen

02.01.08.MaxSumElements

Jul 2nd, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxSumElements
  4. {
  5.     static void Main()
  6.     {
  7.         int[] intArray = { -1, -2, -5 , -11, -1};
  8.  
  9.         int biggerSum = 0;
  10.         int previousSum = 0;
  11.         int currentSum = 0;
  12.         int memberIndication = 0;
  13.         int count = 0;
  14.         int counter = 0;
  15.         bool allNegative = true;
  16.  
  17.         // Finding the sequence of maximal sum
  18.  
  19.         for (int i = 0; i < intArray.Length; i++)
  20.         {
  21.             if (intArray[i] >= 0)
  22.             {
  23.                 allNegative = false;
  24.                 previousSum = currentSum;
  25.                 currentSum = currentSum + intArray[i];
  26.                 count++;
  27.                 if (biggerSum < currentSum)
  28.                 {
  29.                     biggerSum = currentSum;
  30.                     memberIndication = i;
  31.                     counter = count;
  32.                 }
  33.             }
  34.             else if ((i - 1) >= 0 && intArray[i] < 0 && currentSum + intArray[i] < 0)
  35.             {
  36.                 currentSum = 0;
  37.                 previousSum = 0;
  38.                 count = 0;
  39.             }
  40.             else if ((i - 1) >= 0 && intArray[i] < 0 && currentSum + intArray[i] > 0)
  41.             {
  42.                 previousSum = currentSum;
  43.                 currentSum = currentSum + intArray[i];
  44.                 count++;
  45.                 if (biggerSum < currentSum)
  46.                 {
  47.                     biggerSum = currentSum;
  48.                     memberIndication = i;
  49.                     counter = count;
  50.                 }
  51.             }
  52.         }
  53.  
  54.         if (allNegative == true)
  55.         {
  56.             int tempMax = int.MinValue;
  57.             for (int i = 0; i < intArray.Length; i++)
  58.             {
  59.                 if (tempMax < intArray[i])
  60.                 {
  61.                     tempMax = intArray[i];
  62.                 }
  63.             }
  64.             biggerSum = tempMax;
  65.             Console.WriteLine(new string('-', 30));
  66.             Console.WriteLine("The maximal sum of sequential elements in the N-array is = {0} ", biggerSum);
  67.             Console.WriteLine("he sequential elements with the max sum are: {0}", biggerSum);
  68.             Console.WriteLine(new string('-', 30));
  69.             Environment.Exit(0);
  70.         }
  71.         // Print
  72.  
  73.         Console.WriteLine(new string('-', 30));
  74.         Console.WriteLine("The maximal sum of sequential elements in the N-array is = {0} ", biggerSum);
  75.  
  76.         Console.Write("The sequential elements with the max sum are: {");
  77.         for (int i = memberIndication - counter + 1; i <= memberIndication + 1; i++)
  78.         {
  79.             if (biggerSum > 0)
  80.             {
  81.                 Console.Write("{0}, ", intArray[i]);
  82.                 biggerSum = biggerSum - intArray[i];
  83.             }
  84.             else if (biggerSum < 0)
  85.             {
  86.                 break;
  87.             }
  88.         }
  89.         Console.Write("\b\b}\n");
  90.         Console.WriteLine(new string('-', 30));
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement