This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

They probably want you to work out your own algorithm.

You can set a min value which is higher than the minimum, either by using numbers[0] or Integer.MAX_VALUE. Then you iterate the array; if the value you find is smaller than the min value, use that as the new min value. You can also record the index in another variable if you wish. When you get to the end of the array . . . voila!

Per definition the minimum over an empty list / array is +infinity, and the maximum is -infinity. For int, Integer.MAX_VALUE and Integer.MIN_VALUE are the nearest matches for those.

Well that was what I learned at University. With all the theorists, a.k.a. Edsger W. Dijkstra clones. I dare say clones because they have even adopted his handwriting!

All known operators have an identity value, and this is what is returned for empty lists:

+ has 0, since x + 0 == x for all x

* has 1, since x * 1 == x for all x

&& has true, since x && true == x for all x

|| has false, since x || false == x for all x

min has +infinity, since min(x, +infinity) == x for all x

max has -infinity, since max(x, -infinity) == x for all x

This is also used when formally deducing the code for a program. For which Anne Kaldewaij's "Programming: The Derivation of Algorithms" is a great read.

I'm still not there. I understand the logic of the identity value, but I don't get:

...and this is what is returned for empty lists

What do the other operators (+, *, &&, ||) have to do with empty lists. And why would -∞ be the minimum of an empty list? I'd have guessed undefined as Ilja did.

Further this requirement must only refer to lists (sets) of numbers where -∞ is a valid member.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

If you read that book I mentioned you'll understand, but recursion is the basics: min(a[0], a[1], a[2], ..., a[n]) == min(min(a[0], a[1], a[2], ..., a[n - 1]), a[n])

Somewhere this has to end, and Dijkstra and his followers have chosen not to use just an array of size 1 but an empty array as the basis. Basically, they have given undefined a value. And until you have a special symbol for undefined, that makes sense. If the end result of a non-empty array is that value you know that ALL values of the array have that value.

And it's +∞ that is the minimum over an empty list. -∞ would cause problems since -∞ < x for all x. [ October 28, 2008: Message edited by: Rob Prime ]