# Check if a stack of Integers contains a sequence of consecutive values

Write a method isConsecutive that takes a stack of integers as a parameter and that returns whether or not the stack contains a sequence of consecutive integers starting from the bottom of the stack (returning true if it does, returning false if it does not). Consecutive integers are integers that come one after the other, as in 5, 6, 7, 8, 9, etc. So if a stack s stores the following values:

bottom [3, 4, 5, 6, 7, 8, 9, 10] top

Then the call of isConsecutive(s) should return true. If the stack had instead contained this set of values:

bottom [3, 4, 5, 6, 7, 8, 9, 10, 12] top

Then the call should return false because the numbers 10 and 12 are not consecutive. Notice that we look at the numbers starting at the bottom of the stack.

The following sequence of values would be consecutive except for the fact that it appears in reverse order, so the method would return false: bottom [3, 2, 1] top

Your method must restore the stack so that it stores the same sequence of values after the call as it did before. Any stack with fewer than two values should be considered to be a list of consecutive integers. You may use one queue as auxiliary storage to solve this problem.

I'm looking for ways to improve my future code. I was also wondering if it's better practice to use generics in cases like these? I'll also accept other methods of solving this problem as the correct answer. My main goal is to try to take away something that will make me a better programmer; if you have advice to shed, please don't hesitate to share it!

public boolean isConsecutive(Stack<Integer> s) {
boolean consecutive = true;
while (!s.isEmpty()) {
q.offer(s.pop());
}

while (!q.isEmpty()) {
s.push(q.poll());
}

while (!s.isEmpty()) {
q.offer(s.pop());
}

while (q.size() > 1) {
int first = q.poll();
int second = q.poll();
consecutive &= (second - first == 1);
s.push(first);
s.push(second);
}

if (!q.isEmpty()) {
int first = q.poll();
int second = s.peek();
consecutive &= (second - first == 1);
s.push(first);
}
return consecutive;
}

• Have you seen this question from yourself ? Looks like the iteration logic is the same only the comparison is different. You may think of a generic implementation that accept a Comparator or BiPredicate Dec 9, 2017 at 21:05

Bug 1

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(isConsecutive(stack));


returns false.

Bug 2

Your method throws EmptyStackException on stack of size 1.

I suggest you declare your method as static. That way the client programmer does not need to instantiate an object.

Alternative implementation

This is just a slight modification of my answer to your previous stack related question:

public static boolean isConsecutiveV2(Stack<Integer> stack) {
if (stack.size() <= 1) {
return true;
}

Stack<Integer> aux = new Stack<>();

aux.push(stack.pop());

while (!stack.isEmpty() && stack.peek() == aux.peek() - 1) {
aux.push(stack.pop());
}

boolean consecutive = stack.isEmpty();

// Restore the input stack:
while (!aux.isEmpty()) {
stack.push(aux.pop());
}

return consecutive;
}