-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularMovingSum.cs
More file actions
53 lines (44 loc) · 1.65 KB
/
CircularMovingSum.cs
File metadata and controls
53 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
// https://www.sololearn.com/Discuss/666260/?ref=app
namespace SoloLearn
{
class Program
{
static void Main(string[] zephyr_koo)
{
DisplayCircularSum(new int[] { 1, 3, 4 }, 5); // Case 1
DisplayCircularSum(new int[] { 1, 6, 7, 8 }, 3); // Case 2
DisplayCircularSum(new int[] { 4, 5 }, 5); // Case 3
DisplayCircularSum(new int[] { 2, -4, 6, -7 }, 5); // Case 4
}
static void DisplayCircularSum(int[] numList, int count)
{
var len = numList.Length;
var sumArray = Enumerable.Range(0, len)
.Select(n =>
Enumerable.Range(n, count)
.Sum(i => numList[i % len]));
Console.WriteLine(string.Join(", ", sumArray));
}
/// <summary>
/// This alternative was used to demonstrate the idea for the
/// full LINQ solution above if you couldn't follow it.
/// </summary>
/// <param name="numList"></param>
/// <param name="count"></param>
static void DisplayCircularSum_PartialLINQ(int[] numList, int count)
{
var len = numList.Length;
var sumArray = new List<int>();
for (int i = 0; i < len; i++)
{
var partialSum = Enumerable.Range(i, count)
.Sum(n => numList[n % len]);
sumArray.Add(partialSum);
}
Console.WriteLine(string.Join(", ", sumArray));
}
}
}