Advertisement
simonses

MaxSumOfK

Jan 9th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxSumOfK
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         int k = int.Parse(Console.ReadLine());
  9.  
  10.         // Read the array
  11.         int[] array = new int[n];
  12.  
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             array[i] = int.Parse(Console.ReadLine());
  16.         }
  17.  
  18.         // Compair
  19.         int currentSum = 0;
  20.         int sum = 0;
  21.  
  22.         for (int i = 0; i < array.Length - k + 1; i++)
  23.         {
  24.             for (int e = 0; e < k; e++)
  25.             {
  26.                 currentSum += array[i + e];
  27.             }
  28.             if (currentSum >= sum)
  29.             {
  30.                 sum = currentSum;
  31.                 currentSum = 0;
  32.             }
  33.             else
  34.             {
  35.                 currentSum = 0;
  36.             }
  37.         }
  38.         Console.WriteLine(sum);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement