Check if stack elements are pairwise consecutive

1.6

Given a stack of integers, write a function pairWiseConsecutive() that checks whether numbers in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should retain the original stack content.

Only following standard operations are allowed on stack.

  • push(X): Enter a element X on top of stack.
  • pop(): Removes top element of the stack.
  • empty(): To check if stack is empty.

Examples:

Input : stack = [4, 5, -2, -3, 11, 10, 5, 6, 20]
Output : Yes
Each of the pairs (4, 5), (-2, -3), (11, 10) and
(5, 6) consists of consecutive numbers.

Input : stack = [4, 6, 6, 7, 4, 3]
Output : No
(4, 6) are not consecutive.

The idea is to use another stack.

  1. Create an auxiliary stack aux.
  2. Transfer contents of given stack to aux.
  3. Traverse aux. While traversing fetch top two elements and check if they are consecutive or not. After checking put these elements back to original stack.
// C++ program to check if successive
// pair of numbers in the stack are
// consecutive or not
#include <bits/stdc++.h>
using namespace std;

// Function to check if elements are
// pairwise consecutive in stack
bool pairWiseConsecutive(stack<int> s)
{
    // Transfer elements of s to aux.
    stack<int> aux;
    while (!s.empty()) {
        aux.push(s.top());
        s.pop();
    }

    // Traverse aux and see if
    // elements are pairwise
    // consecutive or not. We also
    // need to make sure that original
    // content is retained.
    bool result = true;
    while (aux.empty() > 1) {

        // Fetch current top two
        // elements of aux and check
        // if they are consecutive.
        int x = aux.top();
        aux.pop();
        int y = aux.top();
        aux.pop();
        if (abs(x - y) != 1)
          result = false;

        // Push the elements to original
        // stack.
        s.push(x);
        s.push(y);
    }

    if (aux.size() == 1)
        s.push(aux.top());

    return result;
}

// Driver program
int main()
{
    stack<int> s;
    s.push(4);
    s.push(5);
    s.push(-2);
    s.push(-3);
    s.push(11);
    s.push(10);
    s.push(5);
    s.push(6);
    s.push(20);

    if (pairWiseConsecutive(s))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    cout << "Stack content (from top)"
          " after function call\n";
    while (s.empty() == false)
    {
       cout << s.top() << " ";
       s.pop();
    }

    return 0;
}

Output:

Yes
Stack content (from top) after function call
20 6 5 10 11 -3 -2 5 4 

Time complexity: O(n).
Auxiliary Space : O(n).

This article is contributed by Prakriti Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.6 Average Difficulty : 1.6/5.0
Based on 3 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.