added maxByN and minByN