score:1

Accepted answer

assuming that you have to have an 11-dimensional array (doing some m-theory work?) and that no other data structure is possible, and given that you don't want to use looping to find the maximum value in the array, that leaves you with just one possibility that i can think of:

create a new managedarray class with two private fields, one for values and one for maximums.
create an indexer with 11 index parameters. the get is as simple as anything with 11 parameters can be:

        public int this[int index1, int index2, int index3, int index4, int index5, int index6, int index7, int index8, int index9, int index10, int index11]
        {
            get
            {
                return (int) values.getvalue( index1, index2, index3, index4, index5, index6, index7, index8, index9, index10, index11 );
            }

the set will call setvalue and then update the current maximums with the new value if the new value is bigger than the existing maximum for that set of dimensions.
create a maximum property that takes a bunch of index values, have it return the current value of maximums at those index values. now change arr1 and arr2 to be instances of the managedarray class.

what we've done is to trade a loop at request time (when you want to know the maximum) with a slightly more time-consuming array setter that tracks maximum values so we can look them up in constant time whenever requested.

afaik, that's the only way you can do it.

note that you can make it a lot easier to read by using the params keyword, but then the setter will be a little more complicated when you're updating the maximums. up to you.

score:0

i know this will not really help you, but you can not use linq in this case, because multidimensional arrays are not implementing the ienumerable interface.

so do not waste your time to solve your problem with linq :)

score:0

how can you not iterate over all elements when you need to calculate the maximum value?

imo it would make your life easier if you placed your data in a more accessible data structure, at least something supporting ienumerable<t>.


Related Query