Skip to main content
1 of 5

Java, 101 bytes

a->{int m,n;m=n=a[0];--m;for(int i:a)if(i==m+1)m=i;else if(i==n-1)n=i;else return false;return true;} 

Ungolfed:

a -> { int m, n; m = n = a[0]; --m; for (int i : a) if (i == m + 1) m = i; else if (i == n - 1) n = i; else return false; return true; } 

Keep track of the highest and lowest values seen so far as m and n; only accept a new value if it is m + 1 or n - 1 i.e. the next higher or lower value; initialize the high value, m, to one less than the first element so that it will "match" the first time around the loop. Note: this is a linear-time, online algorithm. It requires only three words of memory, for the current, highest-so-far, and lowest-so-far values, unlike a lot of the other solutions.

Usage:

import java.util.function.Predicate; public class Antsy { public static void main(String[] args) { int[] values = { 6, 5, 4, 7, 3, 8, 9, 2, 1, 0 }; System.out.println(test(values, a -> { int m, n; m = n = a[0]; --m; for (int i : a) if (i == m + 1) m = i; else if (i == n - 1) n = i; else return false; return true; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } }