Related Articles

# Check if moves in a stack or queue are possible or not

• Last Updated : 27 May, 2021

Given a binary array, where 1 denotes push operation and 0 denotes a pop operation in a stack or queue. The task is to check if the possible set of operations are valid or not.
Examples:

Input: a[] = {1, 1, 0, 0, 1}
Output: Valid
Input : a[] = {1, 1, 0, 0, 0}
Output : Invalid
The third pop operation cannot be done as the stack or queue will be empty at that moment of time.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

A naive approach will be to use a stack or queue and perform a push operation when an array element is 1, and perform a pop operation when an array element is 0. When a pop operation is there to be performed, then the move is invalid if the stack or queue is empty. If we can perform all the operations, then the moves are valid.
Time Complexity: O(N)
Auxiliary Space: O(N)
An efficient approach will be counting the push operation and reduce them when a pop operation is performed. If during any instance, the count becomes less than 0, then the set of operations is invalid.
Below is the implementation of the above approach.

## C++

 `// C++ program to Check if moves in a stack``// or queue are possible or not``#include ``using` `namespace` `std;` `// Function to check if``// operations are valid or not``bool` `check(``int` `a[], ``int` `n)``{` `    ``// count number of push operations``    ``int` `ones = 0;` `    ``// traverse in the array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// push operation``        ``if` `(a[i])``            ``ones++;` `        ``// pop operation``        ``else``            ``ones--;` `        ``// if at any moment pop() operations``        ``// exceeds the number of push operations``        ``if` `(ones < 0)``            ``return` `false``;``    ``}` `    ``return` `true``;``}` `// Driver Code``int` `main()``{``    ``int` `a[] = { 1, 1, 0, 0, 1 };``    ``int` `n = ``sizeof``(a) / ``sizeof``(a);``    ``if` `(check(a, n))``        ``cout << ``"Valid"``;``    ``else``        ``cout << ``"Invalid"``;``}`

## Java

 `// Java program to Check if moves in a stack``// or queue are possible or not` `public` `class` `GFG {` `// Function to check if``// operations are valid or not``    ``static` `boolean` `check(``int` `a[], ``int` `n) {` `        ``// count number of push operations``        ``int` `ones = ``0``;` `        ``// traverse in the array``        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``// push operation``            ``if` `(a[i] ==``1``) {``                ``ones++;``            ``} ``// pop operation``            ``else` `{``                ``ones--;``            ``}` `            ``// if at any moment pop() operations``            ``// exceeds the number of push operations``            ``if` `(ones < ``0``) {``                ``return` `false``;``            ``}``        ``}` `        ``return` `true``;``    ``}` `// Driver Code``    ``static` `public` `void` `main(String[] args) {``        ``int` `a[] = {``1``, ``1``, ``0``, ``0``, ``1``};``        ``int` `n = a.length;``        ``if` `(check(a, n)) {``            ``System.out.println(``"Valid"``);``        ``} ``else` `{``            ``System.out.println(``"Invalid"``);``        ``}` `    ``}``}``// This code is contributed by Rajput-Ji`

## Python 3

 `# Python 3 program to Check if moves``# in a stack or queue are possible or not` `# Function to check if``# operations are valid or not``def` `check(a, n):` `    ``# count number of push operations``    ``ones ``=` `0``;` `    ``# traverse in the array``    ``for` `i ``in` `range` `(``0``, n):` `        ``# push operation``        ``if` `(a[i]):``            ``ones ``=` `ones ``+` `1``;` `        ``# pop operation``        ``else``:``            ``ones ``=` `ones ``-` `1``;` `        ``# if at any moment pop() operations``        ``# exceeds the number of push operations``        ``if` `(ones < ``0``):``            ``return` `False``;` `    ``return` `True``;` `# Driver Code``a ``=` `[ ``1``, ``1``, ``0``, ``0``, ``1` `];``n ``=` `len``(a);``if` `(check(a, n)):``    ``print``(``"Valid"``);``else``:``    ``print``(``"Invalid"``);` `# This code is contributed``# by Akanksha Rai`

## C#

 `using` `System;``                    ` `    ` `// C# program to Check if moves in a stack``// or queue are possible or not`` ` `public` `class` `GFG {`` ` `// Function to check if``// operations are valid or not``    ``static` `bool` `check(``int` `[]a, ``int` `n) {`` ` `        ``// count number of push operations``        ``int` `ones = 0;`` ` `        ``// traverse in the array``        ``for` `(``int` `i = 0; i < n; i++) {`` ` `            ``// push operation``            ``if` `(a[i] ==1) {``                ``ones++;``            ``} ``// pop operation``            ``else` `{``                ``ones--;``            ``}`` ` `            ``// if at any moment pop() operations``            ``// exceeds the number of push operations``            ``if` `(ones < 0) {``                ``return` `false``;``            ``}``        ``}`` ` `        ``return` `true``;``    ``}`` ` `// Driver Code``    ``static` `public` `void` `Main() {``        ``int` `[]a = {1, 1, 0, 0, 1};``        ``int` `n = a.Length;``        ``if` `(check(a, n)) {``            ``Console.Write(``"Valid"``);``        ``} ``else` `{``            ``Console.Write(``"Invalid"``);``        ``}`` ` `    ``}``}``// This code is contributed by Rajput-Ji`

## PHP

 ``

## Javascript

 ``
Output:
`Valid`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up