13,042,421 members (137,565 online)
Rate this:
See more:
To select max and min value in a list of items taking a group of n items at a time, so that we have max value min value pairs for every n number of items in the sequence they are added to the group.
For eg if we have total 10 items as
{ 1,2,3,4,5,6,7,8,9,10 }
and we need to take 3 at a time, and get the max , min values, we should get
{ {1,3}, {4,6},{7,9}, {10,10} ) these min,max values.
Posted 2-Sep-12 23:26pm
YvesDaoust 3-Sep-12 5:54am

What is the question ?
Monidipa1 3-Sep-12 5:59am

the question is how to make the final list from the initial one. In the example I have shown numbers 1,2...,10 these are actualy objects of class type
public class ValueTimeStampPair
{
public string szTimeStamp;
public int nValue;
}

and we need to get max,min values according to int nValues
YvesDaoust 3-Sep-12 6:19am

For me it is unclear what you cannot do.

Rate this:

## Solution 1

Let m be the length of the initial list of items. Scan the list using a double loop: the outer loop with (i= 0; i < m; i+= n).

In this outer loop you initialize max and min with the value at i, then compare to the values at j using an inner loop (j= i + 1; j < Min(i + n, m); j++).

Then you can append the max/min pair to the list of pairs.
Rate this:

## Solution 2

Hi,

What you have done so far ? try something yourself and come with some issue/problem. I am posting here the solution but i will also give you another question and expecting to resolve by you.

```class calculate
{
int startIndex = 0;
int count = 3;
List<int[]> myResult = new List<int[]>();

public new List<int[]> MaxMinFinder(int[] abc)
{
int[] value1 = new int[2];
value1[0] = abc.Skip(startIndex).Take(count).Max();
value1[1] = abc.Skip(startIndex).Take(count).Min();
startIndex += count;
if (startIndex <= abc.Length)
{
MaxMinFinder(abc);
}

return myResult;
}
}```

You can use this function to get List<int[]>(your expected result).

- Make above class generalize, i do not want any fix value. in above code you can see count = 3 is fix. make it generalize to use anywhere.

Hope you can do this, even this problem is very simpler then yours,

Best luck
Thanks
-Amit Gajjar

Top Experts
Last 24hrsThis month
 Karthik Bangalore 130 Satya Prakash Swain 130 ppolymorphe 105 OriginalGriff 105 Richard MacCutchan 100
 OriginalGriff 4,215 RickZeeland 1,834 F-ES Sitecore 1,483 ppolymorphe 1,454 Dave Kreskowiak 1,299