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 Articles