Click here to Skip to main content
12,395,871 members (61,899 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.

Solution for your question :
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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 3 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100