Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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.

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.
 
Share this answer
 
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.

Solution for your question :
C#
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();
            myResult.Add(value1);
            startIndex += count;
            if (startIndex <= abc.Length)
            {
                MaxMinFinder(abc);
            }

            return myResult;
        }
    }


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

Now whats your homework is,
- 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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900