# Finding an array index by giving a maximum value

### Question

• Hi there,

.Net 4.0 C#

1) I have an int Array {5, 10, 1, 40, 110, 120, 12, 70}
2) I have an external number ie. 44

Without using loops, is there a possibility to find the index of the highest value in the array that must be lower or equal than the external number (in this case 40, or index 3)?

Ralph

• Moved by Monday, May 23, 2011 2:55 PM (From:.NET 4: Windows Workflow Foundation)
Sunday, May 22, 2011 4:46 PM

• Without any loop, implicit or explicit:

```int Highest(int[] array, int max, int offset)
{
if (array == null || array.Length <= offset || array[offset] > max)
return int.MinValue;
else
return Math.Max(array[offset], Highest(array, max, offset + 1));
}
```
Thursday, May 26, 2011 11:45 AM
• You can use LINQ to find your answer. LINQ will do a loop internally, but I suspect you don't mind.

```  class Program
{
static void Main(string[] args)
{
Int32[] arr = new Int32[] { 5, 10, 1, 40, 110, 120, 12, 70 };

var yournumber = (from nbr in arr
where nbr < 44
select nbr).Max();

Console.WriteLine(yournumber.ToString());

}
}
```

Bob - www.crowcoder.com
Wednesday, May 25, 2011 11:40 AM
• Hi

Use this code :

```int[] intArray = { 5, 10, 1, 40, 110, 120, 12, 70 };
int number = 44;

int maxValue = intArray.Where(i => number >= i).Max();
```

Thursday, May 26, 2011 1:42 PM

### All replies

• Your requirement is a bit special, .NET framework library does not supply a build-in method to perform this. You should write your own logic to fulfill this.
The first solution coming into being is using the Array.Sort() method, in ascending order by default, to sort the array first and then comparing the array items in order, set the current item to be the temp maximum value in every comparison if the item value is less than 44, return when the current item is greater than 44.

While if you pursue the efficiency, don't use the Array.Sort() method and do not need to sort the array, just loop through the array like the following code snippet, this is the most efficient way.

```      Int32[] intArray = { 5, 10, 1, 40, 110, 120, 12, 70 };
Int32 maxValue = -1;

foreach (Int32 item in intArray)
{
if (item > 44) continue;
else
{
if (item > maxValue)
maxValue = item;
}
}
```

Leo Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Wednesday, May 25, 2011 11:05 AM
• You can use LINQ to find your answer. LINQ will do a loop internally, but I suspect you don't mind.

```  class Program
{
static void Main(string[] args)
{
Int32[] arr = new Int32[] { 5, 10, 1, 40, 110, 120, 12, 70 };

var yournumber = (from nbr in arr
where nbr < 44
select nbr).Max();

Console.WriteLine(yournumber.ToString());

}
}
```

Bob - www.crowcoder.com
Wednesday, May 25, 2011 11:40 AM
• This could be an interesting problem.  What is the size of the int array you need to search?  Can it be ordered (sorted) or must the order be random?  Is there a well defined possible range of numbers in the int source array?

Wednesday, May 25, 2011 8:15 PM
• Without any loop, implicit or explicit:

```int Highest(int[] array, int max, int offset)
{
if (array == null || array.Length <= offset || array[offset] > max)
return int.MinValue;
else
return Math.Max(array[offset], Highest(array, max, offset + 1));
}
```
Thursday, May 26, 2011 11:45 AM
• use the array.Last method's second prototype that expects as a parameter a function predicate. You can use the predicate to define the condition of your search.
Thursday, May 26, 2011 12:00 PM
• Last returns the last, not the max. You'd need to sort or order the array first.
Thursday, May 26, 2011 1:10 PM
• So a recursive method that stacks up int arrays is better than:

var yournumber = (from nbr in arr
where nbr < 44
select nbr).Max();
?

Bob - www.crowcoder.com
Thursday, May 26, 2011 1:33 PM
• Hi

Use this code :

```int[] intArray = { 5, 10, 1, 40, 110, 120, 12, 70 };
int number = 44;

int maxValue = intArray.Where(i => number >= i).Max();
```

Thursday, May 26, 2011 1:42 PM
• Not better, but it answers the question of how to do it without loops.  I was hoping for more information from the OP to determine if it even matters.  Unless the array is huge a single for loop is the way to solve his problem.  Your solution has two loops with is unnecessary if you want to split hairs.
Thursday, May 26, 2011 1:47 PM
• Not better, but it answers the question of how to do it without loops.  I was hoping for more information from the OP to determine if it even matters.  Unless the array is huge a single for loop is the way to solve his problem.  Your solution has two loops with is unnecessary if you want to split hairs.

I'd say that ESPECIALLY if the array is huge, a single for loop is the way to solve the problem.

I expect the question is an academic one, though. I like Louis' recursive solution, if that's the case. :)

Thursday, May 26, 2011 3:03 PM
• Note that no arrays are stacked. Arrays are reference types.
Thursday, May 26, 2011 4:13 PM
• Bad choice of words. I didn't mean stacked as in put on the Stack. So the arrays are "heaped up". I just don't want anyone to get the impression that there is no cost to recursion.
Bob - www.crowcoder.com
Friday, May 27, 2011 10:45 AM
• It is indeed good to remember that each call to that Highest method needs at least 20 bytes. For an array of 1000000 items, that's 19MB. Using a loop is the good choice.

Friday, May 27, 2011 4:14 PM