Steps to do:
- Split an array into destination number of arrays (later called: subarrays). In this case you should get three 9-elements subarrays
- Loop through the data of each subarray and group data by using modulo of counter and the number of consecutive numbers to skip
- Calculate the sum for each group
Linq solution:
int[] arr = new int[]{1,3,2,4,6,7,8,9,10,11,12,16,5,8,7,8,4,6,8,9,10,11,12,16,2,4,5};
int ConsecutiveNoToSkip = 2;
int NoOfGroups = 3;
int ItemsInGroup = arr.Length/NoOfGroups;
for(int i=0; i<NoOfGroups; i++)
{
var grp = arr.Skip(ItemsInGroup*i)
.Take(ItemsInGroup)
.Select((x, counter) => new
{
Number=x,
Counter=counter,
GrpNo=counter%(ConsecutiveNoToSkip+1)
})
.GroupBy(x=>x.GrpNo)
.Select(g=>new
{
Numbers = String.Join("+", g.Select(x=>x.Number)),
Result = g.Sum(y=>y.Number)
});
}
Result:
subarray #1
Numbers Result
1+4+8 13 //not 15!
3+6+9 18
2+7+10 19
subarray #2
Numbers Result
11+5+8 24
12+8+4 24
16+7+6 29
subarray #3
Numbers Result
8+11+2 21
9+12+4 25
10+16+5 31