Check if moves in a stack or queue are possible or not
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.
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 <bits/stdc++.h> 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[0]); 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
[tabby title="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
<?php // PHP program to Check if moves in a // stack or queue are possible or not // Function to check if // operations are valid or not function check( $a , $n ) { // count number of push operations $ones = 0; // traverse in the array for ( $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 $a = array ( 1, 1, 0, 0, 1 ); $n = count ( $a ); if (check( $a , $n )) echo "Valid" ; else echo "Invalid" ; // This code is contributed by Ryuga ?> |
Valid
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Check if a queue can be sorted into another queue using a stack
- Stack and Queue in Python using queue Module
- Implement Stack and Queue using Deque
- Implement a stack using single queue
- How to implement stack using priority queue or heap?
- Difference between Stack and Queue Data Structures
- Stack Permutations (Check if an array is stack permutation of other)
- Level order traversal in spiral form | Using one stack and one queue
- Check if Queue Elements are pairwise consecutive | Set-2
- Check if Queue Elements are pairwise consecutive
- Check for balanced parenthesis without using stack
- Check if an array is stack sortable
- Check if the elements of stack are pairwise sorted
- Check if stack elements are pairwise consecutive
- Check if the given push and pop sequences of Stack is valid or not
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.