Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class MaxSum
- {
- static void Main()
- {
- uint arrayLength = 0;
- int pointer = 0;
- int currentSum = 0;
- int maxSum = 0;
- List<int> indices = new List<int>();
- Console.Write("Enter the arrays's length: ");
- while (!uint.TryParse(Console.ReadLine(), out arrayLength))
- {
- Console.Write("Invalid input. Enter a positive integer number: ");
- }
- int elementsCount = 0;
- Console.Write("Enter the count of the elements to be summed: ");
- while (!int.TryParse(Console.ReadLine(), out elementsCount) || elementsCount > arrayLength)
- {
- Console.Write("Invalid input. Enter a positive integer number: ");
- }
- int[] array = new int[arrayLength];
- for (int i = 0; i < arrayLength; i++)
- {
- Console.Write("Enter the {0} element of the array: ", i);
- while (!int.TryParse(Console.ReadLine(), out array[i]))
- {
- Console.Write("Invalid input. Enter an integer number: ");
- }
- }
- for (int i = 0; i <= arrayLength - elementsCount; i++)
- {
- for (int j = i; j < i + elementsCount; j++)
- {
- currentSum += array[j];
- }
- if (maxSum < currentSum)
- {
- maxSum = currentSum;
- indices.Clear();
- indices.Add(i);
- }
- else if (maxSum == currentSum)
- {
- indices.Add(i);
- }
- currentSum = 0;
- }
- if (indices.Count == 1)
- {
- Console.Write("There is one sequence of {0} elements with maximal sum {1}: ", elementsCount, maxSum );
- }
- else
- {
- Console.WriteLine("There are {0} sequences of {1} with maximal sum {2}: ", indices.Count, elementsCount, maxSum);
- }
- for (int i = 0; i < indices.Count; i++)
- {
- int[] maxSubsetSum = new int[elementsCount];
- for (int j = 0; j < elementsCount; j++)
- {
- maxSubsetSum[j] = array[indices[i]];
- indices[i]++;
- }
- Console.WriteLine(string.Join(", ", maxSubsetSum));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement