Check if moves in a stack or queue are possible or not
Last Updated :
13 Sep, 2022
Given a binary array, where 1 denotes a push operation and 0 denotes a pop operation in a stack or queue. The task is to check if the possible set of operations is 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), as we will be using a loop to traverse N times where N is the number of elements in the array.
Auxiliary Space: O(N), as we will be using extra space for the stack or queue.
An efficient approach will be counting the push operation and reducing 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++
#include <bits/stdc++.h>
using namespace std;
bool check( int a[], int n)
{
int ones = 0;
for ( int i = 0; i < n; i++) {
if (a[i])
ones++;
else
ones--;
if (ones < 0)
return false ;
}
return true ;
}
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
public class GFG {
static boolean check( int a[], int n) {
int ones = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 1 ) {
ones++;
}
else {
ones--;
}
if (ones < 0 ) {
return false ;
}
}
return true ;
}
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" );
}
}
}
|
Python 3
def check(a, n):
ones = 0 ;
for i in range ( 0 , n):
if (a[i]):
ones = ones + 1 ;
else :
ones = ones - 1 ;
if (ones < 0 ):
return False ;
return True ;
a = [ 1 , 1 , 0 , 0 , 1 ];
n = len (a);
if (check(a, n)):
print ( "Valid" );
else :
print ( "Invalid" );
|
C#
using System;
public class GFG {
static bool check( int []a, int n) {
int ones = 0;
for ( int i = 0; i < n; i++) {
if (a[i] ==1) {
ones++;
}
else {
ones--;
}
if (ones < 0) {
return false ;
}
}
return true ;
}
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" );
}
}
}
|
PHP
<?php
function check( $a , $n )
{
$ones = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ])
$ones ++;
else
$ones --;
if ( $ones < 0)
return false;
}
return true;
}
$a = array ( 1, 1, 0, 0, 1 );
$n = count ( $a );
if (check( $a , $n ))
echo "Valid" ;
else
echo "Invalid" ;
?>
|
Javascript
<script>
function check(a , n) {
var ones = 0;
for (i = 0; i < n; i++) {
if (a[i] == 1) {
ones++;
}
else {
ones--;
}
if (ones < 0) {
return false ;
}
}
return true ;
}
var a = [ 1, 1, 0, 0, 1 ];
var n = a.length;
if (check(a, n)) {
document.write( "Valid" );
} else {
document.write( "Invalid" );
}
</script>
|
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times, where N is the number of elements in the array.
- Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...