Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MaxSumConsElements
- {
- static void Main()
- {
- Console.Write("Enter the array length: ");
- int length = int.Parse(Console.ReadLine());
- int[] array = new int[length];
- Console.Write("Enter the number of consequtive elements for finding the max sum: ");
- int elements = int.Parse(Console.ReadLine());
- long totalSum = int.MinValue;
- int startElement = 0;
- for (int i = 0; i < length; i++)
- {
- Console.Write("Enter elements[{0}] value: ", i + 1);
- array[i] = int.Parse(Console.ReadLine());
- }
- for (int i = 0; i < length-elements; i++)
- {
- int currSum = 0;
- for (int g = 0; g < elements; g++)
- {
- currSum += array[i + g];
- }
- if (currSum > totalSum)
- {
- totalSum = currSum;
- startElement = i;
- }
- }
- Console.Write("The {0} element with maximal sum in the array are: ",elements);
- for (int i = 0; i < elements; i++)
- {
- Console.Write("{0} ",array[startElement+i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement