Checking if numbers in array are sorted or reverse sorted I would like to determine is an array of numbers, is always increasing (for all x, x+1 >= x) or always decreasing (for all x, x+1 =&lt; x). If so return true. Another way to state the problem is, return true if the array is sorted or reverse sorted. Here is the code:

public static boolean isAlwaysIncreasingOrDecreasing(int[] arr) {
        //check if decreasing
        int previous = arr[0];
        boolean decreasing = true;
        for(int i = 1; i &lt; arr.length; i++) {
            if(previous &lt; arr[i]) {
                decreasing = false;
                break;
            }   
            previous = arr[i];
        }       

        //check if increasing
        int previous = arr[0];
        boolean increasing = true;
        for(int i = 1; i &lt; arr.length; i++) {
            if(previous &gt; arr[i]) {
                increasing = false;
                break;
            }   
            previous = arr[i];
        }

        //return true if always increasing or decreasing
        if(decreasing || increasing) {
            return false;
        }

        return true;
}


Any tips? Obviously duplicate values are allowed, so inputs such as 1,2,3,3,3,3....3,4,2,5,6 would be tricky.

I ask mainly out of curiosity, as the above solution would take at most 2 iterations through the array and I don't think it is possible to take less than 1, so it's not too bad :)
Your version is slightly misnamed.  It's actually isNonincreasingOrNondecreasing.  It returns true for arrays where all the entries are the same value, which is neither increasing nor decreasing.  

If you don't want to duplicate the checks, you can add a bit more logic.

public static boolean isNonincreasingOrNondecreasing(int[] sequence) {
    // clear out equal entries from the beginning
    int i = 1;
    while (i &lt; sequence.length &amp;&amp; sequence[i - 1] == sequence[i]) {
        i++;
    }

    // if there are no entries left, then it is nonincreasing and nondecreasing
    if (i &gt;= sequence.length) {
        return true;
    }

    int previous = sequence[i - 1];
    if (i &lt; sequence.length &amp;&amp; previous &gt; sequence[i]) {
        for (; i &lt; sequence.length; i++) {
            if (previous &lt; sequence[i]) {
                return false;
            }

            previous = sequence[i];
        }

        return true;
    }

    for (; i &lt; sequence.length; i++) {
        if (previous &gt; sequence[i]) {
            return false;
        }

        previous = sequence[i];
    }

    return true;
}


This version only loops once, although it has three loops coded.  It only runs one loop over any section of the array.  

It also gets rid of the Boolean variables.  It can return immediately since it only gets to the loops once it already knows which one to test.