Sign up ×

When I study algorithms from a textbook, usually the algorithm being in pseudocode is as generic as possible.

An example is that in order to simplify checks or boundary cases or when to stop a loop the occusionally -/+ infinity is being used (as a simplification of course).

For example:

current-sum=enter image description here

for i=x downto low  
     if(total-sum > current-sum)  //so that in first iteration we will enter the if statement


Ok, the minus infinity can be replaced when implementing the algorithm in a programming language with a value not expected in our domain of problem.

I was wondering though if there is a more generic way/trick to represent this when implementing the algorithm in a concrete programming language, for example Java, than doing:
current-sum = -1; or
current-sum = -10000;

where for instance these values could later actually become valid as domain values.

share|improve this question
I don't understand what the pseudo code is doing or why. This makes it difficult to translate. – Peter Lawrey Oct 29 '11 at 8:58
The pseudocode is part of the algorithm for computing the max subarray problem. In this part it calculates sums in the array and uses -infinity as a starting point.If you think it makes sense I can post more of the pseudocode – Cratylus Oct 29 '11 at 9:05

5 Answers 5


In general no. There is no way to represent "minus infinity" as an integer that will always work. The smallest possible integer value (in Java Integer.MIN_VALUE) is often used. But you need to look at the specific algorithm to understand if this solution works.

Hence there is no general / generic solution.

In your example Integer.MIN_VALUE can be used in place of "minus infinity". However, if an algorithm requires you to use Integer.MIN_VALUE as an actual value, then you can't also use it as a "no value" marker. Another case where Integer.MIN_VALUE doesn't work as a proxy for "minus infinity" is when you use it to do arithmetic. (Integer.MIN_VALUE + x == Integer.MIN_VALUE only holds for x == 0.)

share|improve this answer
+1 for expanded treatment of generic problem – Cephron Oct 29 '11 at 9:18

You can use Integer.MIN_VALUE which is the smallest 32-bit representable value (-2^31).

Alternatively, if this could actually be a valid value in your problem, you could extend to 64 bits and just use Long.MIN_VALUE.

And finally, there's always the option of boolean firstTime = true; which you || against your if condition the first time around, and immediately set it to false.

share|improve this answer
int current_sum = Integer.MIN_VALUE;

This gives current_sum the minimum value an int can have, -(2^31).

share|improve this answer

Normally you are representing infinity with the maximum that can fit in your datatype.

For example if you have +inifity, you can use Integer.MAX_VALUE. Same with -infinity, you can use Integer.MIN_VALUE.

It's the same with other datatypes.

share|improve this answer

Integer.MIN_VALUE and Integer.MAX_VALUE bad solutions because in some cases they can become values in your domain. More carefully using Double.NEGATIVE_INFINITY.


share|improve this answer
That's interesting, I never knew about that property. Cool. – Cephron Oct 29 '11 at 9:12

Your Answer


