Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Write a program that reads two integer numbers N and K and an array of N elements from the console. Find in the array those K elements that have maximal sum.
- class SumOfArray
- {
- static void Main()
- {
- //Input
- int n = int.Parse(Console.ReadLine());
- int k = int.Parse(Console.ReadLine());
- int sum = 0;
- int maxSum = 0;
- int minIndex = 0;
- int[] array = new int[n];
- for (int i = 0; i < n; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- //Solution
- for (int i = 0; i <= (n - k); i++)
- {
- for (int j = i; j < (i + k); j++)
- {
- sum += array[j]; // Current sum
- }
- if (sum > maxSum)
- {
- maxSum = sum;
- minIndex = i; // Get min index of array
- }
- sum = 0;
- }
- //Output
- for (int i = minIndex; i < (minIndex + k); i++)
- {
- Console.Write("{0} ",array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement