Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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.
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- namespace TryHere
- {
- class TryHere
- {
- static void Main(string[] args)
- {
- Stopwatch st = new Stopwatch();
- st.Start();
- //int n = 10;
- int k = 3;
- int[] arr = new int[] { 3,6,1,8,-1,8,11,5,10,-12,100,1,14,65,77,-1,5,23,43,12};
- Queue<int> q = new Queue<int>();
- StringBuilder sb = new StringBuilder();
- string result = string.Empty;
- int maxSum = int.MinValue;
- for (int i = 0; i < arr.Length; i++)
- {
- q.Enqueue(arr[i]);
- if (q.Count==k)
- {
- int sum = 0;
- //sum all integers in queue
- foreach (var item in q)
- {
- sum += item;
- sb.Append(item + " ");
- }
- if (sum > maxSum)
- {
- maxSum = sum;
- result = sb.ToString();
- }
- sb.Clear();
- q.Dequeue();
- }
- }
- Console.WriteLine(result);
- st.Stop();
- TimeSpan ts = st.Elapsed;
- Console.WriteLine(ts);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement