Note: this can also be written without taking advantage of Java 8 lambdas:
Java 7, 89 bytes
boolean c(int[]a){int n=a[0],m=n-1;for(int i:a)n-=i==m+1?m-m++:i==n-1?1:n+1;return n==0;} Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalNote: this can also be written without taking advantage of Java 8 lambdas:
boolean c(int[]a){int n=a[0],m=n-1;for(int i:a)n-=i==m+1?m-m++:i==n-1?1:n+1;return n==0;} Note: this can also be written without taking advantage of Java 8 lambdas:
boolean c(int[]a){int n=a[0],m=n-1;for(int i:a)n-=i==m+1?m-m++:i==n-1?1:n+1;return n==0;} a->{int n=a[0],m=n-1;for(int i:a)if(i==m+1)m=i;else n=i==nn-1=i==m+1?im-m++:i==n-1;return1?1:n+1;return n==0;} Saved 1923 bytes withthanks to excellent suggestions from Peter Taylor.!
a -> { int n = a[0], m = n - 1; for (int i : a) ifn (-= i == m + 1)? m = i; else- nm++ =: i == n - 1? i1 : -n + 1; return n == 0; } (Unfortunately this is less efficient because we always have to look at the entire sequence rather than bailing out after the first wrong number, but it's hard to argue with a 1923-byte savings (!) when other solutions are using O(n^2) and exponential time approaches.)
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 n = a[0], m = n - 1; for (int i : a) ifn (-= i == m + 1)? m = i; else- nm++ =: i == n - 1? i1 : -n + 1; return n == 0; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } } a->{int n=a[0],m=n-1;for(int i:a)if(i==m+1)m=i;else n=i==n-1?i:-1;return n==0;} Saved 19 bytes with suggestions from Peter Taylor.
a -> { int n = a[0], m = n - 1; for (int i : a) if (i == m + 1) m = i; else n = i == n - 1? i : -1; return n == 0; } (Unfortunately this is less efficient because we always have to look at the entire sequence rather than bailing out after the first wrong number, but it's hard to argue with a 19-byte savings when other solutions are using O(n^2) and exponential time approaches.)
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 n = a[0], m = n - 1; for (int i : a) if (i == m + 1) m = i; else n = i == n - 1? i : -1; return n == 0; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } } a->{int n=a[0],m=n-1;for(int i:a)n-=i==m+1?m-m++:i==n-1?1:n+1;return n==0;} Saved 23 bytes thanks to excellent suggestions from Peter Taylor!
a -> { int n = a[0], m = n - 1; for (int i : a) n -= i == m + 1? m - m++ : i == n - 1? 1 : n + 1; return n == 0; } (Unfortunately this is less efficient because we always have to look at the entire sequence rather than bailing out after the first wrong number, but it's hard to argue with a 23-byte savings (!) when other solutions are using O(n^2) and exponential time approaches.)
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 n = a[0], m = n - 1; for (int i : a) n -= i == m + 1? m - m++ : i == n - 1? 1 : n + 1; return n == 0; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } } a->{int n=a[0],m=n-1;for(int i:a)if(i==m+1)m=i;else n=i==n-1?i:-1;return n==0;} Formerly:
Saved 19 bytes with suggestions from Peter Taylor.
Ungolfed:
a -> { int m, n; n = a[0], m = n = a[0]; --m; 1; for (int i : a) if (i == m + 1) m = i; else ifn (= i == n - 1)? ni =: i;-1; else return 0>1; n return== 1>0;0; } 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.
If the next value misses both the high and low ends of the range, the lowest-so-far value is set to -1 and then the low end can never proceed and reach zero. We then detect an antsy sequence by checking whether the low value, n, reached zero.
(Unfortunately this is less efficient because we always have to look at the entire sequence rather than bailing out after the first wrong number, but it's hard to argue with a 19-byte savings when other solutions are using O(n^2) and exponential time approaches.)
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; n = a[0], m = n = a[0]; --m; 1; for (int i : a) if (i == m + 1) m = i; else ifn (= i == n - 1)? ni =: i;-1; else return 0>1; n return== 1>0;0; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } } 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 0>1; return 1>0; } 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.
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 0>1; return 1>0; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } } a->{int n=a[0],m=n-1;for(int i:a)if(i==m+1)m=i;else n=i==n-1?i:-1;return n==0;} Formerly:
Saved 19 bytes with suggestions from Peter Taylor.
Ungolfed:
a -> { int n = a[0], m = n - 1; for (int i : a) if (i == m + 1) m = i; else n = i == n - 1? i : -1; return n == 0; } 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.
If the next value misses both the high and low ends of the range, the lowest-so-far value is set to -1 and then the low end can never proceed and reach zero. We then detect an antsy sequence by checking whether the low value, n, reached zero.
(Unfortunately this is less efficient because we always have to look at the entire sequence rather than bailing out after the first wrong number, but it's hard to argue with a 19-byte savings when other solutions are using O(n^2) and exponential time approaches.)
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 n = a[0], m = n - 1; for (int i : a) if (i == m + 1) m = i; else n = i == n - 1? i : -1; return n == 0; } )); } public static boolean test(int[] values, Predicate<int[]> pred) { return pred.test(values); } }