Check If Stack Elements Are Pairwise Consecutive in Java



Stacks are a fundamental data structure in computer science, often used for their Last-In-First-Out (LIFO) properties. One interesting problem that can be encountered while working with stacks is checking whether the elements of a stack are pairwise consecutive. In this article, we will learn to solve this problem using Java, ensuring an efficient and clear solution.

Problem Statement

Given a stack of integers, the task is to determine if the elements of the stack are pairwise consecutive. Two elements are considered consecutive if they differ by exactly 1.

Input

4, 5, 2, 3, 10, 11

Output

Are elements pairwise consecutive?
true

Steps to check if stack elements are pairwise consecutive

Following are the steps to check if stack elements are pairwise consecutive ?

  • Check the Stack Size: If the stack has an odd number of elements, the last element will not have a pair, so it should be ignored for the pairwise check.
  • Pairwise Check: Loop through the stack, popping elements in pairs, and checking if they are consecutive.
  • Restore the Stack: After performing the check, the stack should be restored to its original state.

Java program to check if stack elements are pairwise consecutive

Below is the program in Java to check if stack elements are pairwise consecutive ?

import java.util.Stack;
public class PairwiseConsecutiveChecker 
{
    public static boolean areElementsPairwiseConsecutive(Stack<Integer> stack) {
        // Base case: If the stack is empty or has only one element, return true
        if (stack.isEmpty() || stack.size() == 1) {
            return true;
        }

        // Use a temporary stack to hold elements while we check
        Stack<Integer> tempStack = new Stack<>();
        boolean isPairwiseConsecutive = true;

        // Process the stack elements in pairs
        while (!stack.isEmpty()) {
            int first = stack.pop();
            tempStack.push(first);

            if (!stack.isEmpty()) {
                int second = stack.pop();
                tempStack.push(second);

                // Check if the pair is consecutive
                if (Math.abs(first - second) != 1) {
                    isPairwiseConsecutive = false;
                }
            }
        }

        // Restore the original stack
        while (!tempStack.isEmpty()) {
            stack.push(tempStack.pop());
        }

        return isPairwiseConsecutive;
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(4);
        stack.push(5);
        stack.push(2);
        stack.push(3);
        stack.push(10);
        stack.push(11);

        boolean result = areElementsPairwiseConsecutive(stack);

        System.out.println("Are elements pairwise consecutive? " + result);
    }
}

Explanation

Restoring the stack: Since we modify the stack while checking the pairs, it is important to restore it to its original state after the check is complete. This ensures that the stack remains unchanged for any subsequent operations.

Edge cases: The function handles edge cases such as an empty stack or a stack with only one element, returning true since these cases trivially satisfy the condition.

Time complexityThe time complexity of this approach is O(n), where n is the number of elements in the stack. This is because we only traverse the stack once, popping and pushing elements as needed.

Space complexityThe space complexity is also O(n) due to the use of a temporary stack.

Conclusion

This solution provides an efficient way to check if the elements in a stack are pairwise consecutive. The key is to process the stack in pairs and ensure the stack is restored to its original state after the operation. This approach maintains the integrity of the stack while providing a clear and effective solution to the problem.

Updated on: 2024-09-19T22:00:31+05:30

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements